Vim Tips Wiki
Register
Advertisement
Tip 120 Printable Monobook Previous Next

created September 24, 2001 · complexity basic · author Anon · version 6.0


The $VIMRUNTIME/compiler has jikes.vim, but there's nothing for traditional Sun JDK (javac), so I tried (only tested on Windows):

" Vim Compiler File javac.vim
" Compiler: Sun/IBM JDK: Javac

if exists("current_compiler")
  finish
endif
let current_compiler = "javac"

" Javac defaults to printing output on stderr and no options can convert,
" so we have to set 'shellpipe'
setlocal shellpipe=2>
" 2> works on Win NT and UNIX
setlocal makeprg=javac\ #<.java
setlocal errorformat=%f:%l:%m
" I'm not familiar with 'errorformat', so I set it very simple.

Alternative procedure

TODO: The following is from a similar tip (tip 849 which has now been removed). This material should be merged with the above information.

Add the following to your vimrc file to map F9 to compile, and F10 to run. You can also use F11 and F12 to compile and run the alternate file. I've found this very useful when I am working on a Java class in one file, and a driver program to test it in the alternate file.

See also VimTip3 (using Jikes).

" F9/F10 compile/run default file.
" F11/F12 compile/run alternate file.

map <F9> :set makeprg=javac\\ %<CR>:make<CR>
map <F10> :!echo %\\|awk -F. '{print $1}'\\|xargs java<CR>
map <F11> :set makeprg=javac\\ #<CR>:make<CR>
map <F12> :!echo #\\|awk -F. '{print $1}'\\|xargs java<CR>

map! <F9> <Esc>:set makeprg=javac\\ %<CR>:make<CR>
map! <F10> <Esc>:!echo %\\|awk -F. '{print $1}'\\|xargs java<CR>
map! <F11> <Esc>set makeprg=javac\\ #<CR>:make<CR>
map! <F12> <Esc>!echo #\\|awk -F. '{print $1}'\\|xargs java<CR>

" Tip: load a file into the default buffer, and its driver
" into the alternate buffer, then use F9/F12 to build/run.
" Note: # (alternate filename) isn't set until you :next to it!
" Tip2: You can make then run without hitting ENTER to continue. F9-F12

" With these you can cl/cn/cp (quickfix commands) to browse the errors
" after you compile it with :make

set makeprg=javac\\ %
set errorformat=%A:%f:%l:\\ %m,%-Z%p^,%-C%.%#

" If two files are loaded, switch to the alternate file, then back.
" That sets # (the alternate file).
if argc() == 2
  n
  e #
endif

Comments

I got error:

no alternative filename to substitute for '#'

To make it work, I replaced the alternate file name (#) with the real file name (%) on the makeprg line:

setlocal makeprg=javac\ %

The version of javac I have (JDK1.4) now has an option to send compiler messages to a file instead of to stderr:

javac -Xstdout jerrors.txt ...

Note that any -X option is nonstandard. However, since I'm on Win98 and its DOS shell is so flaky (no way to redirect stderr), -Xstdout was a boon.


I made a small adjustment to the errorformat listed in the Vim documentation, it shows the symbol in the 'symbol: blabla' line as part of the error message (%m).

There is still one problem with %p, don't know if it can be fixed easily: the javac compiler outputs the line with the error and uses the caret (^) to indicate the correct location of the error.

But it uses spaces, not tabs. Since I use tabs for my indentation, the column number is wrong.

:setlocal errorformat=%A%f:%l:\ %m,%-Z%p^,%Csymbol\ \ :\ %m,%-C%.%#

and it should output something like this :

Composition.java|10 col 34| cannot resolve symbol class ClassNameHere

Try these commands (compiler\ant.vim) to fix the problem when using quickFix with ant and windows (standard error cannot be redirect).

setlocal makeprg=ant\ -l\ .ant.log
set makeef=.ant.log
set shellpipe=

To redirect stderr under Windows (not Windows 98/ME):

:set shellpipe=>\ %s\ 2>&1

Then, :make will be expanded to {makeprg} > {errorfile} 2>&1

For Windows 98/ME, you might try a handy utility called stderr, which directs errorsteram to stdout. Can be found at http://www.teaser.fr/~amajorel/stderr/

The errorformat I'm using:

%A%f:%l:\ %m,%-Z%p^,%-C%.%#

If you have foo.java after you run javac you do java foo NOT java foo.class else you get the exception in thread main message.


Advertisement