Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Java code in Vim more efficiently

Tags:

java

vim

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 MyClass.java

followed by

! java -cp . MyClass

If I have compile errors, I have to go back to the compiler output using ! and manually jump to every line that produced an error. And as soon as I start adding other classes, I have to compile each of them separately.

There's got to be a more efficient way than this. Under my current inefficient Vim workflow, I can get stuff done faster in a graphical IDE, which beats the purpose of using Vim for me.

I'd like to be able to enter something like :compile in the class containing my main method to compile all my sources and be presented with a split-screen list of error messages. What would you recommend?


Related, but not relevant to me personally:

  • Tips for using vim as a Java IDE?
  • Programming Java with Vim

Update: My takeaway from this question is posted as a separate answer.

like image 775
Pieter Avatar asked Jun 20 '11 13:06

Pieter


4 Answers

By request I've posted my takeaway from this question as a separate answer.


Here's how I used everyone's advice.

Walking through compile errors quickly

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.

like image 159
Pieter Avatar answered Nov 13 '22 03:11

Pieter


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
like image 29
rekinyz Avatar answered Nov 13 '22 03:11

rekinyz


Here the vim wiki article for compiling with javac.

like image 32
jamapag Avatar answered Nov 13 '22 05:11

jamapag


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 / :cnewerto go back to earlier/later quickfix lists. Quickfix will remember where in the quickfix stack you were for a specific quickfix list.

like image 3
sehe Avatar answered Nov 13 '22 04:11

sehe