1

有人我用这个:

org.apache.commons.jci.compilers.JavacJavaCompiler

我有一个 jci 课程:

public void compile() {
    sourceDir = new File("java");
    sources = new String[] { "test/HelloWorld.java" };
    target = new File("result");

    JavacJavaCompilerSettings settings = new JavacJavaCompilerSettings();

    JavacJavaCompiler javacJavaCompiler = new JavacJavaCompiler(settings);

    System.out.println("compile -> " + sources);
    System.out.println("compile -> " + sourceDir);
    System.out.println("compile -> " + target);

    CompilationResult result = javacJavaCompiler.compile(sources,
            new FileResourceReader(sourceDir),
            new FileResourceStore(target));

    for (CompilationProblem cp : result.getErrors()) {
        System.out.println("compile -> " + cp.getStartLine());
        System.out.println("compile -> " + cp.getMessage());
    }
}

我的输出是:

 compile -> 0
 compile -> Failure executing javac, but could not parse the error: javac: file not found:     test/HelloWorld.java
 Usage: javac <options> <source files>
 use -help for a list of possible options

一些想法,泰。

4

1 回答 1

1

输出清楚地告诉您,编译器无法使用您提供的路径名找到您的源文件。两个可能的原因浮现在脑海中:

  • 您提供了错误的源文件路径名,或者
  • JVM 的“当前目录”不是包含源路径名中使用的“测试”目录的目录。

要尝试的事情:

  • 对源文件使用绝对路径名,只是为了证明您可以运行编译器。
  • 打印出System.getProperty("user.dir")... 的值,它应该与 JVM 的当前目录匹配。在此基础上,找出源文件的相对路径名应该是什么。
于 2011-02-22T03:01:24.477 回答