views:

912

answers:

6
+2  Q: 

Faster javac/ant?

I find java starts up and runs practically instantly for me - but javac takes a few seconds, and ant makes it slower again. It's only a few seconds, but my edit-compile-test loop would be smoother without it. :-)

BTW: I've already used vim's ":make" with ant.

Is there a way to speed up javac and/or ant? I'm thinking of special switches, or tricks? Or maybe an alternative java compiler (I'm using 1.6, in linux)

+1  A: 

See this question - Is there a way to improve the multicore or multiprocessor performance of the Java compiler?

Elijah
Thanks, but I'm on a single core machine.
13ren
+1  A: 

Eclipse does that for you ... but it's probably a bit big as a "patch" for your problem.

That aside, you can roll your own compiler plugin. There are two approaches:

  1. Run the java compiler from within ant (instead of creating a new process). Not sure if ant already does that; if not, that will save you a bit of time. Look at the Java 6 compiler API for details.

  2. Run javac in a server process which listens for options on a socket. In ant, send the process the command line and wait for the output to be sent back. Saves you starting a new process all the time.

  3. Try to use the eclipse compiler. Unlike the original javac, the Eclipse compiler is pretty good at "ignoring" errors, so it can produce class files even when they contain errors. That doesn't seem to mean much but it allows you to compile all the time in the background. When you do your last save (wrapping everything up), the compiler will have been able to compile everything else and will just have to look at a single file.

Aaron Digulla
Thanks, 1. I think there is an option for whether ant forks or not. 2. Setting up a java server process is a cute idea, though I'm not sure how to go about it :-). 3. Most of the time (not always) there is already only one file to compile (though I see that the compiler would also already be loaded, too)
13ren
I wrote a javac server (2), and compilation now feels instant! It's really cool. (about 0.5 sec; down from 3-4 sec). The server invokes JavaCompiler.compile when it's sent javac options, and redirects stderr to the client, so the client's behaviour is indistinguishable from javac (just faster). Not integrated with ant, because ant itself adds 1-2 sec (and I can get by without ant), but I could tell ant my client is a compiler. I stop the server with "pkill java" - I guess sending a "stop" message is the right way.
13ren
Loading the Java compiler takes a lot of time (it has to setup the environment, load all the classes for the VM, allocate lots of memory, etc.). Loading the (few) files to compile is actually pretty fast.
Aaron Digulla
A: 

You may want to have a look at JavaRebel

Jorn
A: 

I can strongly recommend using a suitable IDE with Java as the productivity increase with using an editor which knows about your program is immense. Think of "goto the line containing the defintion of the variable/class/field the cursor is on", "rename this class and all references to it", and all other kinds of nice things. If you are annoyed with the time it takes to invoke Java, you might be ready :)

Thorbjørn Ravn Andersen
The question is about compiling, not invoking java ("java starts up and runs practically instantly"). With ^], vim + ctags will "goto the line containing the definition of the variable/class/field the cursor is on". I found IDEs more annoying and less productive (a week trial), even though I expected them to be very helpful.
13ren
Even if java starts up and runs practically instantly, javac doesn't which is what you are complaining about.Did you try to learn an IDE on your own, or did an experienced developer mentor you?
Thorbjørn Ravn Andersen
Having asked the question, I am aware of that. I was pointing out that you didn't appear to have read the question. I'm not sure I want to have a conversation with you, as you appear to be quite arrogant, with your "you might be ready" line. Oh well, I'll give you a go: I tried the IDE on my own without an experienced user of it. However, in learning vim, I had many experienced users around, so this probably helped a lot. From your answer, you seem unfamiliar with vim.
13ren
Anyone who does Java programming for a living waste his own time and his employers money by not using a modern IDE.
Thorbjørn Ravn Andersen
Thanks for clearing that up. Have a nice day.
13ren
No problem. If you still need a fast, incremental compiler, have a look at Jikes.
Thorbjørn Ravn Andersen
+1  A: 

Google found these two (I haven't tried either yet)

  1. javac -J-client -J-Xms100m -J-Xmx100m <src>

  2. JBrownie "monitors Java sourcecode and automatically recompiles any files found changed" along the lines of Aaron Digulla's (2)

13ren
A: 

I point all of my output files to a ramdisk that I create with this utility. It speeds up builds a fair bit (Though not to a magical extent) since almost everything is in memory. It's most noticeable when doing a 'clean' build which becomes almost instant.

izb