Why does javac recompile my .java although the .class is already updated?
سؤال
I am told that javac
is smart enough that it does not recompile the .java
, in condition that this .java
has a timestamp before that of .class
. I just find it does not work like that in my machine, am I certainly wrong somewhere?
my P.java
is located under .../eg/access
, with P.java
declared as a package of access
eg
|- access
|- P.class
|- P.java
Then when I run javac P.java
, each time it is recompiled, is that normal or am I wrong?
المحلول
It's normal that it is recompiled each time. javac doesn't do incremental compilation.
نصائح أخرى
Javac by default recompiles everything each time. The behaviour you observe is correct, and by design.
It looks like you can specify an option for this behaviour though:
-Xprefer:{newer,source}
Specify which file to read when both a source file and class file are found for a type. (See Searching For Types). If -Xprefer:newer is used, it reads the newer of the source or class file for a type (default). If the -Xprefer:source option is used, it reads source file. Use -Xprefer:source when you want to be sure that any annotation processors can access annotations declared with a retention policy of SOURCE.
Taken from: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
You may also be thinking of Eclipse's compiler, which I'm pretty sure does do incremental compilation - or the javac task in ant which is smart enough to figure out such cases.
I think you might be confusing javac
as the command line executable with the javac
task in Apache Ant. javac
recompiles the source file always, no matter whether it is older or newer than the binary file. Ant is smart enough to figure out when the file has to be recompiled, but of course you have to use the Ant build system in order to make use of this feature.