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

アイデア、ty。

4

1 に答える 1

1

出力は、指定したパス名でコンパイラがソース ファイルを見つけられないことを明確に示しています。次の 2 つの原因が考えられます。

  • 間違ったソース ファイルのパス名を指定したか、または
  • ソース パス名で使用される「テスト」ディレクトリを含むディレクトリではなく、JVM の「現在のディレクトリ」。

試すこと:

  • コンパイラを実行できることを証明するためだけに、ソース ファイルの絶対パス名を使用します。
  • ...の値を出力しますSystem.getProperty("user.dir")。これは、JVM の現在のディレクトリと一致する必要があります。それに基づいて、ソース ファイルの相対パス名を特定します
于 2011-02-22T03:01:24.477 に答える