我試圖使用JavaCompiler.run()來編譯動態生成的源文件。 下面是我的程序,當我運行它,我上了System.err的:調用JavaCompiler.run()時未找到javac文件
的javac:未找到文件:C:.. bla..bla .. \測試\ A.java C:.. bla..bla .. \測試\ B.java C:.. bla..bla .. \測試\ C.java 用法:javac的 使用-help爲可能的選項
的文件列表沒有編制,但eveything得到了編譯,當我把在命令提示符下
的javac C:.. bla..bla .. \測試\ A.java C:.. bla..bla .. \測試\ B.jav一個C:.. bla..bla .. \測試\ A.java
下面是完整的源代碼
// define the classes
String stringA = "package test;" + " "
+ "public class A { "
+ "public A() { B b = new B(); b.b(); }" + " "
+ "public static boolean a() { return true;}" + " "
+ "}";
String stringB = "package test;" + " "
+ "public class B { "
+ "public B() { }" + " "
+ "public boolean b() { return new C().c(); }" + " "
+ "}";
String stringC = "package test;" + " "
+ "public class C { "
+ "public C() { }" + " "
+ "public boolean c() { return A.a(); }" + " "
+ "}";
// Save sources in .java files then compile
String pathA = store("test/A.java", stringA);
String pathB = store("test/B.java", stringB);
String pathC = store("test/C.java", stringC);
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(pathA);
pathBuilder.append(" ");
pathBuilder.append(pathB);
pathBuilder.append(" ");
pathBuilder.append(pathC);
String files = pathBuilder.toString();
// run compiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, files);
...
商店(文件名,源)保存文件源並返回存儲文件的完整路徑。