我被告知 javac
足够聪明,它不会重新编译 .java
,前提是这个 .java
有时间戳在 .class
之前。我只是发现它在我的机器上不起作用,我肯定在某个地方错了吗?
我的P.java
位于.../eg/access
下,其中P.java
声明为access
eg
|- access
|- P.class
|- P.java
然后,当我运行javac P.java
时,每次都会重新编译,这是正常的还是我错了?
最佳答案
默认情况下,Javac 每次都会重新编译所有内容。您观察到的行为是正确的,并且是有意设计的。
看起来您可以为此行为指定一个选项:
-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.
取自:http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
您可能还会想到 Eclipse 的编译器,我很确定它确实会进行增量编译 - 或者 ant 中的 javac 任务,它足够智能来找出此类情况。
关于java - 尽管 .class 已经更新,为什么 javac 仍重新编译我的 .java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8271282/