【发布时间】:2015-11-24 01:00:55
【问题描述】:
我已经生成了可以在命令行上使用 javac 编译器编译的代码,如下所示:
javac generated/Buffer.java -classpath framework-core.jar
现在我正在尝试使用 JavaCompiler 自动编译测试。使用以下代码(来自http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html):
String path = "/generated/Buffer.java";
File file = new File(System.getProperty("user.dir") + path);
File[] files1 = new File[]{file};
//HERE I WOULD LIKE TO GIVE framework-core.jar TO COMPILER
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1));
compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
fileManager.close();
如果我不提供 framework-core.jar 的类路径,我会产生与命令行相同的错误。使用程序化 JavaCompiler 时,如何为该 jar 提供类路径?
我还想知道是否有更直接的方法来编译和运行生成的代码。
【问题讨论】:
-
我想知道这些是否有帮助:stackoverflow.com/questions/1563909/…
标签: java code-generation java-compiler-api