48

I have tested many editors out there, but Vim makes me addictive. I really wish to use Vim in all of my programming. I just tried to start learning the Java programming language and I have a huge book that I'm trying to start to read for that purpose, but the sad thing about it is that I don't have either a Java compiler or a .class file reader (called a JVM I think, but maybe I am just a noob too and I already have them but don't know how they work).

I do not wish to use another editor than Vim, that is not my purpose. I know that Vim can call external commands to compile and that I might have to enter some stuff in my _vimrc to let him do so (I use Windows Vista, sad, I know) but I have no idea what to look for and I have searched for days. Any Vim-lovers experts around that could give me a hand out? I see that little Vim command called :javac and it pisses me off to know it's not working yet...

zenazn
  • 14,295
  • 2
  • 36
  • 26

7 Answers7

29

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?

Community
  • 1
  • 1
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • For downloading the Java SE Development Kit - ok Modifying my path to include bin folder to call javac : Can you explain? I don't know Vim THAT well. I only know that I'll need to write some stuff, but I have no idea of what I'd have to write.. –  Jul 06 '09 at 02:31
  • @Patrick, path is more of Windows thing, not vi. See http://www.computerhope.com/issues/ch000549.htm for instruction on how to modify environment variable. You would want to add one called JAVA_HOME and set it to C:\Program Files\Java\jdk1.6.0_14 or whatever appropriate, and add %JAVA_HOME%\bin to PATH separating it with semicolon. This may be done automatically. – Eugene Yokota Jul 06 '09 at 02:40
  • @Patrick, I am branching out the whole environment variable thing to another question: http://stackoverflow.com/questions/1085224/java-what-are-the-necessary-environment-variables-one-should-set-after-installin – Eugene Yokota Jul 06 '09 at 02:52
24

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.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 4
    If someone downvotes this, can they at least explain why ? I think it's a valid contribution to this question – Brian Agnew Dec 17 '12 at 16:43
  • Upvoted since I do the exact same thing. But normally just use Eclipse for java development, use vim for scripting / dynamic languages such as Ruby, javascript, python, etc. – dardo Mar 14 '13 at 19:45
  • 7
    @BrianAgnew, I didn't downvote myself, but I can understand why: the question specifically says "I do not wish to use another editor". At any rate, the answer gives correct information. – Dacav Nov 15 '13 at 15:41
  • I did downvote because for the past 10 years I have been using vim and multiple times I tried to use other ides I even purchased jetbrains license for clion yet I could not use it for more than a week. The performance drop was huge my fingers were simply looking for my custom bindings and enhancements. Vim plugins as you suggest fine until some point. The main downfall of all those plugins and main thing that makes vim more powerful than most other ides is the ability to script easily not the modal layout only – nikoss Feb 27 '21 at 04:15
  • 2
    This is no longer a relevant answer. The primary point made here is the benefit of having Language Server functionality. With COC and Neovim's build in LSP this is no longer the case. You can have full code completion, navigation, documentation etc just like any other IDE eight inside of Vim8 or Neovim – Riley Hughes Feb 02 '22 at 14:45
16

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

oscarkuo
  • 10,431
  • 6
  • 49
  • 62
  • +1 eclim is a project which needs some love from community. It supports completion and shows errors for java and scala. Those two features i could not find anywhere else and imho they are absolutely necessary. – FUD May 23 '13 at 15:56
5

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!

Ratzkewatzke
  • 288
  • 1
  • 3
  • I think I might like your method better than eed3si9n's (no offense, but I'm not used to modifying environment variables and last time I tried it turned out to be gibberish so..) The only thing I don't get is : Supposing that I would have everything installed and set up correctly... Why do I need the JDK??? For me, right now, it just looks as a thing stuck on my computer. I am okay with using the ':make' command, I just thought ':javac' was the command I had to make working to be able to compile. –  Jul 06 '09 at 02:49
  • You will definitely need to have a compiler: something has to turn the Java code into bytecode so that it can be run by the Java virtual machine. Vim won't do that, so eed3si9n's solution is definitely the beginning of any solution for you. Vim is a nice editor and it has lots of neat features, but it doesn't compile code. It has some nice commands set up to invoke the compiler for you, and the :javac and :make commands are of that sort. – Ratzkewatzke Jul 06 '09 at 02:55
  • This whole Ant thing is not user-friendly. I tried to install it but I just gave up noticing I needed help badly... and what I did was the following : I downloaded 'apache-ant-1.7.1-bin.zip' from http://ant.apache.org/bindownload.cgi and extracted it in my computer. Afterwards, in the website you linked me to (http://ant.apache.org/manual/install.html), it said in the 'setup' section : -Add the bin directory to your path. What path!! I don't know what I have to do in this step. –  Jul 06 '09 at 03:36
  • It seems you can't use 'enters' in comments.. that is bad. (following my last comment) 2. Set the ANT_HOME environment variable to the directory where you installed Ant. -Where I installed ant : What folder is 'the place where I installed Ant'? is it the bin folder? the location of some .exe or .bat file? I have no idea... 3. Do I really have to do that environment variable change? I never like to change environment variables because everytime I tried to do so when some programs asked me to, it never seemed to work correctly, so it looks kind of creepy. Same thing for the JAVA_HOME... –  Jul 06 '09 at 03:39
  • Well now I understand a bit better what eed4si9n was trying to say but I thought he was telling me to use another program than vim, but what he actually meant is that Ant would be my compiler.. I think. Everything's not clear right now. –  Jul 06 '09 at 03:40
  • Why are you even trying to use Ant? It only turns things more complex. You just need the JDK, it has the compiler and all the java libraries needed. It's just like C or iphone development, you need the gcc/libraries or iphone SDK. Sometimes I use vim to code Java and it's as simple has editing the file, and then type javac classname.java. The javac is part of the JDK. – rogeriopvl Jul 06 '09 at 08:39
  • If the JDK has a compiler, why the hell didn't someone tell me? That's all I need!... –  Jul 06 '09 at 16:34
  • Oh my god. Such thanks. Rogerio you saved my life. DAMN ALL OF YOU FOR ANT, it's horror for starters! Way too complicated. –  Jul 06 '09 at 16:38
3

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.

schubySteve
  • 707
  • 1
  • 4
  • 9
2

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.

stephan f
  • 589
  • 5
  • 16
-1

Using Vim to write complex Java applications is a nice dream, but it's gonna be a short one :) I know you must be used with VIM(which is fine) but why not using a modern IDE to write a Java application? Most of the current IDEs allows to change the keyboard mapping, so you could bring VIM to your IDE editor...

A few years ago we had a new employee which had a similar dream...after one month he left, I think he realized changing an application with 0.5mil LOC cannot be done in Vim.

Just my $0.02.

adrian.tarau
  • 3,124
  • 2
  • 26
  • 29
  • Actually you can, it depends on how well you can integrate the tools that the Linux environment offers. On vi, I can get code completion, class lookup, and macros for refactoring. Granted, it doesn't have the GUI indicators of compilation errors or context-specific stuff, so you'd have to a coding guru to work in such an manner - but then again, if you are a coding guru, it's silly not to work in an IDE that's makes you work even better. It's only for idealists :) – aberrant80 Jul 06 '09 at 03:27
  • 2
    If you actually take the time and learn the tools, you can be much faster and productive then an IDE user. With Emacs/Vim there is nothing you can't do that an IDE can do. But there are lots of things emacs/vim allows me to do that an IDE can not provide. – Hamza Yerlikaya Jul 06 '09 at 07:16
  • @Hamza can you please give me an example? This is similar with this scenario : in 100 year we can talk with computers but I refuse to do that because I got used with keyboards(not that I believe the keyboards will disappear). My point is, if you like Vim, make your IDE to behave like Vim in those aspects which make you want to work in Vim, but still take advantage of modern IDEs to make you life easier.There is no reason to want to go back in time(70s-80s)...except to explore some beautiful memories from back then :) – adrian.tarau Jul 06 '09 at 13:18
  • 2
    @aberrant80 actually you get compilation errors and context-specific stuff within VIM as well :). Check https://github.com/scrooloose/syntastic/ – Dhruva Sagar Feb 07 '12 at 17:07