Vim Tips Wiki

Compile Java with Sun JDK javac

Revision as of 04:22, September 13, 2012 by (Talk)

1,614pages on
this wiki
Tip 120 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0

The $VIMRUNTIME/compiler directory 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")
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
  e #


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 :|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

The errorformat I'm using:

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

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

This is what I got working in Windows 7 (non cygwin environment) [9/11/2012] to compile java 6 locally with one keypress:

Pressing <F6> compiles Java silently so you won't have to hit enter 2X (once for the terminal window, once in Vim showing you yes this is done). Then an autocommand opens a cwindow (quicklist showing your errors) if there are any errors, so you can see a list of errors.
'Control + J' and 'Control + K' switch between upper and lower window (.java file and error window).
'Control + B' deletes a buffer (i.e. closes Errorlist). I don't really use the 'control + b' to page down, so I reasoned this was a good shortcut. If you like the regular 'contrl + b' shortcut, change mine to <C-delete> or something. I'm sure this could be easily modified to work with jikes.

set makeprg=javac
set makeef=System.err
set shellpipe=
noremap <buffer> <F6> :w<cR>:silent :make "%:p" -Xstdout "%:p:h\System.err"<Cr>
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost    l* nested lwindow
nmap <buffer> <silent> <C-j> :wincmd j<Cr>
nmap <buffer> <silent> <C-k> :wincmd k<Cr>
map <buffer> <silent> <C-b> :bd<Cr>

Around Wikia's network

Random Wiki