ant javac任务使用哪个javac.exe?


问题内容

我面临一个问题。我javac.exe在机器上重命名,并注意到antjavac任务仍然可以正常工作。

有人知道从哪里获取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);
        }
    }

代码来自这里

这意味着,如果库tools.jar位于Ant的当前类路径上,它将拾取该类并启动它。这导致javac.exe可以重命名为您想要的任何名称,它仍然可以工作。因此,要回答您的问题,它实际上不执行任何“ javac.exe”。