views:

255

answers:

2

Recently I started to use Eclipse's java compiler, because it is significantly faster than standard javac. I was told that it's faster because it performs incremental compiling. But I'm still a bit unsure about this since I can't find any authoritative documentation about both - eclispse's and sun's - compilers "incremental feature". Is it true that Sun's compiler always compiles every source file and Eclipse's compiler compile only changed files and those that are affected by such a change?

Edit: I'm not using Eclipse autobuild feature but instead I'm setting

-Dbuild.compiler=org.eclipse.jdt.core.JDTCompilerAdapter

for my ant builds.

+1  A: 

Eclipse certainly does this. Also it does it at save time if you have that option turned on (and it is by default). It looks like sun also doesn't do this (it is very easy to test, just make a small project where A is the main class that uses class B, but B doesn't use class A. Then change A and compile the project again, see if the timestamp for b.class has changed.

This is the way many compilers work (also gcc for instance). You can use tools like ant and make to compile only the part the project that has changed. Also note that these tools aren't perfect, sometimes eclipse just loses track of the changes and you'll need to do a full rebuild.

Thirler
+4  A: 

Is it true that Sun's compiler always compiles every source file and Eclipse's compiler compile only changed files and those that are affected by such a change?

I believe that you are correct on both counts.

You can of course force Eclipse to recompile everything.

But the other part of the equation is that Java build tools like Ant and Maven are capable of only compiling classes that have changed, and their tree of dependent classes.

EDIT

In Ant, incremental compilation can be done in two ways:

  • By default the <javac> task compares the timestamps of .java and corresponding .class files, and only tells the Java compiler to recompile source files that are newer than their corresponding object files, or that don't have an object file at all.

  • The <depend> task also takes into account dependencies between classes, which it determines by reading and analysing the dependency information embedded in the .class files. Having determined which .class files are out of date, the <depend> task deletes them so a following <javac> task will recompile them. However, this is not entirely fool-proof. For example, extensive changes to the source code can lead to the <depend> task may be analysing stale dependencies. Also certain kinds of dependency (e.g. on static constants) are not apparent in the .class file format.

Stephen C
Can you explain how does partial compiling with Ant work? How does Ant force Sun's compiler to compile only changed files and to ignore other files in the source tree?
calavera.info
I suppose that you answered my original question, but as you provide more information I'm getting more confused. If I understand it right it seems to me that javac task without previous clean is very dangerous operation, because it doesn't compile dependent classes if their timestamp isn't changed. (Funny, I just realized that I propably had this problem yesterday and it looked like a total mystery to me.)
calavera.info