Vim Tips Wiki
No edit summary
 
m (Reverted edits by 94.71.135.117 (talk | block) to last version by 71.195.221.197 (script))
Tag: apiedit
 
(14 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=120
 
|id=120
  +
|previous=119
|title=Compiling Java with Sun JDK (javac) within VIM
 
  +
|next=121
|created=September 24, 2001 0:44
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=a jdk user :)
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=134/46
 
|rating=134/46
  +
|category1=Compiler
|text=
 
  +
|category2=Java
The $VIMRUNTIME/compiler has 'jikes.vim', but there's nothing for traditional Sun JDK(javac),
 
 
}}
 
The <code>$VIMRUNTIME/compiler</code> directory has <code>jikes.vim</code>, but there's nothing for traditional Sun JDK (javac), so I tried (only tested on Windows):
  +
<pre>
 
" Vim Compiler File javac.vim
 
" Compiler: Sun/IBM JDK: Javac
   
 
if exists("current_compiler")
so I tried (Only tested on Win 2000):
 
  +
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.
  +
</pre>
   
  +
==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.
" Vim Compiler File javac.vim
 
   
  +
See also [[VimTip3]] (using Jikes).
" Compiler: Sun/IBM JDK: Javac
 
   
  +
<pre>
  +
" 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>
if exists("current_compiler")
 
  +
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
finish
 
  +
" 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
endif
 
  +
" after you compile it with :make
   
let current_compiler = "javac"
+
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
  +
</pre>
   
 
==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:
" Javac defaults to printing output on stderr and no options can convert, so we have to set 'shellpipe'
 
 
setlocal makeprg=javac\ %
   
setlocal shellpipe=2&gt;
 
 
" 2&gt; works on Win NT and UNIX
 
 
setlocal makeprg=javac\ &#35;&lt;.java
 
 
setlocal errorformat=%f:%l:%m
 
 
" Sorry I'm not familiar with 'errorformat', so I set it very simple.
 
 
 
}}
 
 
== Comments ==
 
If you have a Win 9x or ME, you might want to try a handy little utility called stderr, which will direct errorsteram to stdout. can be found at http://www.teaser.fr/~amajorel/stderr/
 
 
The errorformat I'm using (copied it from somewhere):
 
%A%f:%l:\ %m,%-Z%p^,%-C%.%&#35;
 
 
 
bjorklid at jyu.fi
 
, October 19, 2001 10:13
 
----
 
i am running VIM 6 on Redhat Linux 7.0, i get this error on using the above said. Any solutions ?
 
 
no alternative filename to substitute for '&#35;'
 
, January 25, 2002 0:40
 
 
----
 
----
 
The version of javac I have (JDK1.4) now has an option to send compiler messages to a file instead of to stderr:
I also got the same error. To make it work, I replaced the alternate file name ( &#35; ) with the real file name ( % ) on the makeprg line:
 
 
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.
setlocal makeprg=javac\ %
 
   
rickziegler--AT--attbi.com
 
, February 13, 2002 10:57
 
 
----
 
----
 
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).
The version of javac I have (JDK1.4) now has an option to send compiler messages to a file instead of to stderr:
 
   
 
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
javac -Xstdout jerrors.txt ...
 
 
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.
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 very nearly had to write a Java class to run javac for me and do useful stuff with the error messages!
 
   
 
:setlocal errorformat=%A%f:%l:\ %m,%-Z%p^,%Csymbol\ \ :\ %m,%-C%.%#
laugh--AT--starpower.net
 
, July 21, 2002 10:26
 
----
 
I am running on XP. Is there a way to redirect stderr in XP? I want mine to display on the bottom of my gvim window.
 
   
 
and it should output something like this :
Thanx.
 
   
 
Composition.java|10 col 34| cannot resolve symbol class ClassNameHere
thane
 
   
thane777--AT--yahoo.com
 
, July 25, 2002 23:34
 
 
----
 
----
 
Try these commands (compiler\ant.vim) to fix the problem when using quickFix with ant and windows (standard error cannot be redirect).
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).
 
   
 
setlocal makeprg=ant\ -l\ .ant.log
There is still one problem with %p, don't know if it can be fixed easily:
 
 
set makeef=.ant.log
the javac compiler outputs the line with the error and uses the caret (^) to
 
  +
set shellpipe=
indicate the correct location of the error.
 
   
BUT it uses spaces, not tabs. Since I use tabs for my intendation, the column
 
number is wrong.
 
 
--cut--
 
:setlocal errorformat=%A%f:%l:\ %m,%-Z%p^,%Csymbol\ \ :\ %m,%-C%.%&#35;
 
--end--
 
 
and it should output something like this :
 
 
Composition.java|10 col 34| cannot resolve symbol class ClassNameHere
 
...
 
 
Koenraad Heijlen &lt;vipie AD ulyssis DOT org&gt;
 
, July 30, 2002 7:24
 
 
----
 
----
  +
To redirect stderr under Windows (not Windows 98/ME):
Hi,
 
   
 
:set shellpipe=>\ %s\ 2>&1
Try these commands (compiler\ant.vim) to fix the problem when using quickFix with ant and windows (standard error cannot be redirect).
 
   
 
Then, :make will be expanded to {makeprg} > {errorfile} 2>&1
setlocal makeprg=ant\ -l\ .ant.log
 
set makeef=.ant.log
 
set shellpipe=
 
   
 
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/
have fun,
 
   
 
The errorformat I'm using:
Mohamed,
 
  +
%A%f:%l:\ %m,%-Z%p^,%-C%.%#
   
mohamed1999--AT--free.fr
 
, December 20, 2002 10:08
 
 
----
 
----
 
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.
i can javac my file
 
   
ex: :!javac c:\xxx.java &lt;===it's can work!
 
 
but ican't java my file
 
 
ex: :!java c:\xxx(xxx.class) &lt;===Exception in thread "main" java.lang.NoClassDefFoundError: c:\xxx
 
shell returned 1
 
Hit any key to close this window...
 
 
how can i use java in vim
 
 
please tell me and send to my email because my english is very bad!
 
thanks!
 
 
 
shive_x1--AT--yahoo.com.tw
 
, December 25, 2002 8:00
 
 
----
 
----
  +
This is what I got working in Windows 7 (non cygwin environment) [9/11/2012] to compile java 6 locally with one keypress (this assumes you have the path environment variable pointing to your c:\program files\java\javasdk##\bin folder)
Yes, XP can redirect stderr.
 
   
  +
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.<br />
Just do:
 
  +
'Control + J' and 'Control + K' switch between the upper and lower window (.java file and error window).<br />
:set shellpipe=&gt;\ %s\ 2&gt;&amp;1
 
  +
'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.
   
  +
<pre>
Then, :make will be expanded to {makeprg} &gt; {errorfile} 2&gt;&amp;1
 
  +
set makeprg=javac
 
  +
set makeef=System.err
'''Anonymous'''
 
  +
set shellpipe=
, September 20, 2003 9:56
 
  +
noremap <buffer> <F6> :w<cR>:silent :make "%:p" -Xstdout "%:p:h\System.err"<Cr>
----
 
  +
autocmd QuickFixCmdPost [^l]* nested cwindow
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
 
  +
autocmd QuickFixCmdPost l* nested lwindow
 
  +
nmap <buffer> <silent> <C-j> :wincmd j<Cr>
hadding (at) remove_mailandnews.com
 
  +
nmap <buffer> <silent> <C-k> :wincmd k<Cr>
, November 7, 2003 2:44
 
  +
map <buffer> <silent> <C-b> :bd<Cr>
----
 
  +
</pre>
<!-- parsed by vimtips.py in 0.541967 seconds-->
 

Latest revision as of 14:28, 12 April 2016

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")
  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.


This is what I got working in Windows 7 (non cygwin environment) [9/11/2012] to compile java 6 locally with one keypress (this assumes you have the path environment variable pointing to your c:\program files\java\javasdk##\bin folder)

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