views:

224

answers:

4

Is there any posibility to cache java compilation like ccache does for C or C++ compilation?

I have a lot code to compile, and I compile the same code many times, so it took a lot of time. So I thought to cached it to speed up.

+8  A: 

Eclipse does incremental compile of classes (I guess others do this as well). Only the portion of your class that changes will be compiled. Everything else will not be changed. It works reasonably well in real world projects.

Build tools like Maven and Ant can be configured to compile only changed java files. These do not track dependencies in some scenarios they miss needed compiles. (For example change the signature in a super class.)

Thomas Jung
"Only the portion of your class that changes will be compiled." Cool, didn't know that; I though it just recompiled entire classes. But I guess this is necessary for features like allowing to run code from classes with compilation errors (which the Eclipse debugger sometimes allows).
sleske
A batch compiler could do the same. I suppose it's not very useful to print compiler errors as warnings and throw exception at runtime in this scenario.
Thomas Jung
As another usage of the Eclipse compiler - Tomcat uses it as it's compiler for (jsps).
Jeremy Raymond
@Thomas Jung: Actually, some dynamic languages will do just that. It's just that Java is not a dynamic language :-).
sleske
+2  A: 

The best way would be to factor the shared classes into utility jars that you would simply include in your compile classpath, unless there is really a specific (and good) reason that prevents you from doing so (code duplication is evil and will lead you to doom :)).

Romain
+2  A: 

There's no caching functionality in Sun's JDK itself, as far as I am aware.

Most Java IDEs however will cache compilation, so if you need "compilation on demand", that should do.

Eclipse and Netbeans definitely have this. They will only recompile changed classes (and their dependencies, which they figure out automatically). This is fairly fast for most changes.

sleske
If you use ant as a build system (which Eclipse and Netbeans, amongst other, do under the hood), you'll benefit from "on-demand" compilation. A .class will only get generated if the correspondent .java is newer.
Romain
@Romain: Actually, Eclipse goes beyond what ant does. See Thomas Jung's answer. And while Eclipse can also run ant scripts, it has its own internal compiler which is normally used for compiling projects, so saying "Eclipse uses ant under the hood" is a bit misleading.
sleske
A: 

But the main problem is that, the whole source code has many files in many directories, and there are so many java files. Compilation is done by the ant scripts which are invoked by the make script. It has to be compiled by the Sun's JDK. And what is more... between compilations I have to clean all created *class files.

So I cannot use any of IDE's features.

kogut
Uhm… having to “clean all *.class files” and “cache *.class files” are mutually exclusive. What you want can not be done.
Bombe
Please do not post comments as an answer. Just edit your question (or add a comment to it).
sleske