我面临一个问题。我在我的机器上重命名了 javac.exe
并注意到 ant javac
任务仍然可以正常工作。
有人知道它从哪里获取 javac.exe 吗?
最佳答案
实际上,我相信,默认情况下 Ant 会尝试使用以下代码直接执行 java 编译器类:
try {
Class c = Class.forName ("com.sun.tools.javac.Main");
Object compiler = c.newInstance ();
Method compile = c.getMethod ("compile",
new Class [] {(new String [] {}).getClass ()});
int result = ((Integer) compile.invoke
(compiler, new Object[] {cmd.getArguments()}))
.intValue ();
return (result == MODERN_COMPILER_SUCCESS);
} catch (Exception ex) {
if (ex instanceof BuildException) {
throw (BuildException) ex;
} else {
throw new BuildException("Error starting modern compiler",
ex, location);
}
}
代码来自here .
这意味着如果库 tools.jar 位于 Ant 的当前类路径中,它将选择该类并启动它。这导致 javac.exe 可以重命名为您想要的任何名称,它仍然可以工作。因此,为了回答您的问题,它实际上不执行任何“javac.exe”。
Javac 任务还有其他实现,但我认为这是所有编译器 1.3+ 的默认实现
关于java - ant javac任务使用哪个javac.exe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10543336/