Compiling Java code in Vim more efficiently

后端 未结 5 1310
鱼传尺愫
鱼传尺愫 2021-01-30 03:34

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          


        
相关标签:
5条回答
  • 2021-01-30 03:55

    Here the vim wiki article for compiling with javac.

    0 讨论(0)
  • 2021-01-30 03:56

    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.

    0 讨论(0)
  • 2021-01-30 04:03

    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
    
    0 讨论(0)
  • 2021-01-30 04:11

    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.

    0 讨论(0)
  • 2021-01-30 04:14

    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.

    0 讨论(0)
提交回复
热议问题