Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking javac.exe within Vim

Tags:

vim

javac

I had previously asked a question about using Vim to develop Java, but I ended up realizing JDK works very well and that Ant is wayyy too much complicated for a starter like me. Things got a little bit clearer, and now I have a question again.

When I work with a file such as 'filename.java', I need to be able to use 'javac.exe', which is in the Java Development Kit (JDK), on 'filename.java' (a typical project I could be working on). Normally, to do this outside Vim, I could do

:shell in Vim to access a shell outside Vim

C:\Program Files...\javac C:\home-directory-of-filename.java\filename.java

Which is pretty long, because I must access the javac directory and then write the whole 'filename.java' directory.

Is there anyway to ask Vim to call javac and use it directly on the file that I am working on? I would be pleased if anyone could help. I tried to write a couple of things in my _vimrc file, unsuccessfully. By the way, I am a windows vista user.


2 Answers

You can perform an action against the file open in the current buffer using %. So, if you want to execute javac.exe against your current buffer, you could type:

:!javac %

To make it easier on the fingers, you could create a mapping in your .vimrc like so:

map <LocalLeader>jc :!javac %<CR>

Now simply type \jc (or whatever your local leader is) in your buffer and presto, you've run javac against that file.

like image 150
Drew Olson Avatar answered Mar 02 '26 07:03

Drew Olson


Thanks to Drew Olson, I found my answer. But it was not a system path problem, I simply needed to type correctly the path to javac in the mapping. What I have added in my _vimrc was this line :

map jc :!"C:\Program Files\path-leading-to-javac.exe\javac.exe" %

The path written between "s is read correctly by Vim. I don't know if it is written correctly for master-geeks, but I don't care, because it works for me and I know barely nothing right now. =D

P.S. : For geeks who think noobs know everything, please try to be user-friendly. Popping a website with no indications is not user-friendly. It is only a tip, not an offense. I still appreciate the help.