我正在尝试使用JavaCompiler.run()来编译动态生成的源文件。下面是我的程序,当我运行它时,我得到了 System.err:
javac:找不到文件:C:..bla..bla..\test\A.java C:..bla..bla..\test\B.java C:..bla..bla..\test \C.java 用法:javac 使用 -help 获取可能选项的列表
文件没有编译,但是当我输入命令提示符时,一切都被编译了
javac C:..bla..bla..\test\A.java C:..bla..bla..\test\B.java C:..bla..bla..\test\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);
...
store(filename, source) 保存文件源并返回存储文件的完整路径。