Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming Java with Vim

Tags:

java

vim

People also ask

Can I code Java on Vim?

There's a great vim plugin JavaRun that covers the basics of compiling and running, as well as having abbreviations for common code bits that save typing.

How do I open a Java file in Vim?

Reopen the file by typing vim HelloWorld. java and pressing the Enter key. Insert mode is where you can make changes to a file.

What languages does Vim support?

The latest edition of the widely used Vim text editor now supports both the Lua programming language as well as the latest versions of Python and Perl.

Is Emacs good for Java?

Emacs is a one-fits-all solution to programming and of course it also fits Java. That said, Java is a very special case. As you know, Java has very powerful reflection features and IDEs like Intellij or NetBeans use these to live with the language.


You need Java SE Development Kit (JDK) to develop Java. Download JDK 6 Update 14 from the page. Modify your path to include bin folder so you can call javac, the Java compiler and java command. Another tool you wanna learn is ant, which lets you compile multiple files all at once, run tests, etc.. Also see Configure vi for Java application development.

Edit: Adding link to What are the necessary environment variables one should set after installing JDK on Windows and how?


Can I suggest that you use a decent IDE, and a VIM plugin? I used to be a VIM diehard, and only recently switched to using Eclipse with a VIM editor plugin.

Why? The productivity gains of using a modern IDE are enormous. For example, once you're able to navigate through the code by structure type (e.g. method call to list of implementations etc.) then the ctags mechanism in VIM just isn't enough. Once you use more than a couple of libraries (or stuff you're not familiar with) then the code completion / method suggestion / doc navigation will save you a world of grief.

However I love the VIM method of keyboard navigation etc. Hence I use an IDE with an appropriate plugin.

Here's an answer I provided to a very similar question. That contains the link to the Eclipse VIM plugin. It's not free, but will pay for itself very quickly.


Except for the JDK as mentioned by other answers, you might also want to try eclim. It makes VI a full Java IDE.


As eed3si9n points out, you will need the JDK in order to start compiling your Java programs. But your question seems to be more about setting up your editor so that you can automatically compile Java programs from within it.

There are a couple of ways to approach this, of course. The easiest way is to work out a single command to build the project. In Java, most large projects are compiled with ant, and learning how to use that will serve you very well in the long term.

Once you've set up ant (http://ant.apache.org/manual/install.html to start), you can set vi to automatically call it via the ":make" command, by putting in your _vimrc file:

set makeprg=C:\path-to-ant\ant

So the steps should be:

(1) Get the JDK and learn how to use that.

(2) Get ant and learn how to use that. (*)

(3) Set your vimrc up so that it knows to invoke ant when you type :make.

As always, setting up a system to automate something (like your compiles) is easier when you're already proficient at doing it manually.

(*) Note: My vim doesn't have the javac command, so I'm not sure how to configure the vim options necessary to make sure you load it. If nothing else, it would be good to make sure that your javac was in your PATH at the time you load the editor.

Best wishes!


you are going to need to get the Java Development Kit (JDK) which has the compiler etc, without that the javac command is not going to work.


There's a great vim plugin JavaRun that covers the basics of compiling and running, as well as having abbreviations for common code bits that save typing. Just drop it into %HOME%\vimfiles\plugin on Windows.