I come from an Eclipse background, but I love Vim as a text editor. I\'m currently experimenting with Vim as a Java IDE. Currently I do this to compile:
! javac
Here the vim wiki article for compiling with javac.
By request I've posted my takeaway from this question as a separate answer.
Here's how I used everyone's advice.
Add this to ~/.vimrc
:
autocmd Filetype java set makeprg=javac\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>
F9 to compile, F10/F11 to cycle through errors.
if you don't use any package in your java class, then
//compile
:!javac %
//run
:!java -cp %:p:h %:t:r
map F5 in the .vimrc file to automate the build
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!gcc % -o %<"
exec "!time ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "!time ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!time java -cp %:p:h %:t:r"
elseif &filetype == 'sh'
exec "!time bash %"
elseif &filetype == 'python'
exec "!time python2.7 %"
elseif &filetype == 'html'
exec "!firefox % &"
elseif &filetype == 'go'
exec "!go build %<"
exec "!time go run %"
elseif &filetype == 'mkd'
exec "!~/.vim/markdown.pl % > %.html &"
exec "!firefox %.html &"
endif
endfunc
If you run linux(use bash), here is my setup: to compile and run the class with your main file just include this in your vimrc file
nnoremap <leader>ac :cd %:p:h <CR> :! javac %:t<CR> :! java %:t:r<CR>
Let me run you through how this code works. When you press the leader key followed by a and c (\ac), the code changes to the directory of the current file that is open in vim(presumably your main). Then, the code compiles yourfile.java (%:t). Finally, the code runs your main file yourfile (%:t:r). < CR > stands for carriage return, which is the equivalent of enter. This approach would work for developing projects with multiple classes as well, because you could add more code to the above line to make it compile other classes before running main.
With Makefiles, you could use some very generic things:
JAVAFILES=$(wildcard *.java)
mytarget: $(JAVAFILES)
javac $^
On the other hand, you would probably fine doing
:compiler javac
:se makeprg=javac\ **/*.java
:make
:copen
Map some keys to :cnext
and :cprevious
to navigate errors quickly.
Use :colder
/ :cnewer
to go back to earlier/later quickfix lists. Quickfix will remember where in the quickfix stack you were for a specific quickfix list.