Vim Tips Wiki
(Added to Compiler and Java Categories)
m (Reverted edits by 94.71.135.117 (talk | block) to last version by 71.195.221.197 (script))
Tag: apiedit
 
(13 intermediate revisions by 6 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):
so I tried (Only tested on Win 2000):
 
 
<pre>
 
<pre>
" Vim Compiler File javac.vim
+
" Vim Compiler File javac.vim
" Compiler: Sun/IBM JDK: Javac
+
" Compiler: Sun/IBM JDK: Javac
   
if exists("current_compiler")
+
if exists("current_compiler")
  +
finish
finish
 
endif
+
endif
let current_compiler = "javac"
+
let current_compiler = "javac"
   
" Javac defaults to printing output on stderr and no options can convert, so we have to set 'shellpipe'
+
" Javac defaults to printing output on stderr and no options can convert,
 
" so we have to set 'shellpipe'
setlocal shellpipe=2&gt;
+
setlocal shellpipe=2>
" 2&gt; works on Win NT and UNIX
+
" 2> works on Win NT and UNIX
setlocal makeprg=javac\ &#35;&lt;.java
 
setlocal errorformat=%f:%l:%m
+
setlocal makeprg=javac\ #<.java
  +
setlocal errorformat=%f:%l:%m
" Sorry I'm not familiar with 'errorformat', so I set it very simple.
+
" I'm not familiar with 'errorformat', so I set it very simple.
 
</pre>
 
</pre>
}}
 
== 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/
 
   
  +
==Alternative procedure==
The errorformat I'm using (copied it from somewhere):
 
  +
''TODO: The following is from a similar tip (tip 849 which has now been removed). This material should be merged with the above information.''
%A%f:%l:\ %m,%-Z%p^,%-C%.%&#35;
 
   
  +
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).
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 ?
 
   
  +
<pre>
no alternative filename to substitute for '&#35;'
 
  +
" F9/F10 compile/run default file.
, January 25, 2002 0:40
 
  +
" F11/F12 compile/run alternate file.
----
 
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:
 
setlocal makeprg=javac\ %
 
   
  +
map <F9> :set makeprg=javac\ %<CR>:make<CR>
rickziegler--AT--attbi.com
 
  +
map <F10> :!echo %\|awk -F. '{print $1}'\|xargs java<CR>
, February 13, 2002 10:57
 
  +
map <F11> :set makeprg=javac\ #<CR>:make<CR>
----
 
  +
map <F12> :!echo #\|awk -F. '{print $1}'\|xargs java<CR>
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 ...
 
   
  +
map! <F9> <Esc>:set makeprg=javac\ %<CR>:make<CR>
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!
 
  +
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
laugh--AT--starpower.net
 
  +
" into the alternate buffer, then use F9/F12 to build/run.
, July 21, 2002 10:26
 
  +
" Note: # (alternate filename) isn't set until you :next to it!
----
 
  +
" Tip2: You can make then run without hitting ENTER to continue. F9-F12
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.
 
   
  +
" With these you can cl/cn/cp (quickfix commands) to browse the errors
Thanx.
 
  +
" after you compile it with :make
   
 
set makeprg=javac\ %
thane
 
  +
set errorformat=%A:%f:%l:\ %m,%-Z%p^,%-C%.%#
   
  +
" If two files are loaded, switch to the alternate file, then back.
thane777--AT--yahoo.com
 
  +
" That sets # (the alternate file).
, July 25, 2002 23:34
 
  +
if argc() == 2
----
 
  +
n
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).
 
  +
e #
  +
endif
  +
</pre>
   
 
==Comments==
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
 
  +
I got error:
indicate the correct location of the 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:
BUT it uses spaces, not tabs. Since I use tabs for my intendation, the column number is wrong.
 
 
setlocal makeprg=javac\ %
   
--cut--
+
----
 
The version of javac I have (JDK1.4) now has an option to send compiler messages to a file instead of to stderr:
:setlocal errorformat=%A%f:%l:\ %m,%-Z%p^,%Csymbol\ \ :\ %m,%-C%.%&#35;
 
 
javac -Xstdout jerrors.txt ...
--end--
 
   
 
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.
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
 
 
----
 
----
 
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).
Hi,
 
   
 
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
Try these commands (compiler\ant.vim) to fix the problem when using quickFix with ant and windows (standard error cannot be redirect).
 
 
indicate the correct location of the error.
setlocal makeprg=ant\ -l\ .ant.log
 
set makeef=.ant.log
 
set shellpipe=
 
   
 
But it uses spaces, not tabs. Since I use tabs for my indentation, the column number is wrong.
have fun,
 
   
 
:setlocal errorformat=%A%f:%l:\ %m,%-Z%p^,%Csymbol\ \ :\ %m,%-C%.%#
Mohamed,
 
   
 
and it should output something like this :
mohamed1999--AT--free.fr
 
, December 20, 2002 10:08
 
----
 
i can javac my file
 
   
 
Composition.java|10 col 34| cannot resolve symbol class ClassNameHere
ex: :!javac c:\xxx.java &lt;===it's can work!
 
   
 
----
but ican't java my file
 
 
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
ex:
 
 
set makeef=.ant.log
:!java c:\xxx(xxx.class) &lt;===Exception in thread "main" java.lang.NoClassDefFoundError: c:\xxx
 
  +
set shellpipe=
   
 
----
shell returned 1
 
  +
To redirect stderr under Windows (not Windows 98/ME):
Hit any key to close this window...
 
   
 
:set shellpipe=>\ %s\ 2>&1
how can i use java in vim
 
   
 
Then, :make will be expanded to {makeprg} > {errorfile} 2>&1
please tell me and send to my email because my english is very bad!
 
thanks!
 
   
 
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:
shive_x1--AT--yahoo.com.tw
 
 
%A%f:%l:\ %m,%-Z%p^,%-C%.%#
, December 25, 2002 8:00
 
----
 
Yes, XP can redirect stderr.
 
 
Just do:
 
:set shellpipe=&gt;\ %s\ 2&gt;&amp;1
 
 
Then, :make will be expanded to {makeprg} &gt; {errorfile} 2&gt;&amp;1
 
   
'''Anonymous'''
 
, September 20, 2003 9:56
 
 
----
 
----
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
+
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.
   
hadding (at) remove_mailandnews.com
 
, November 7, 2003 2:44
 
 
----
 
----
  +
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)
<!-- parsed by vimtips.py in 0.541967 seconds-->
 
   
  +
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 />
[[Category:Compiler]]
 
  +
'Control + J' and 'Control + K' switch between the upper and lower window (.java file and error window).<br />
[[Category:Java]]
 
  +
'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>
  +
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>
  +
</pre>

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>