在使用JavaCompiler从另一个应用程序中编译类文件之后,如何运行它?
我目前有以下代码:在使用JavaCompiler从另一个应用程序中编译类文件之后,如何运行它?,java,compilation,Java,Compilation,我目前有以下代码: private void compile(){ List<File> files = getListOfJavaFiles(); //JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); //compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
private void compile(){
List<File> files = getListOfJavaFiles();
//JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
//compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjectsFromFiles(files);
List<String> optionList = new ArrayList<String>();
// set compiler's classpath to be same as the runtime's
optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));
//need to add options here.
compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1).call();
//compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
// fileManager.close();
}
private void compile(){
List files=getListOfJavaFiles();
//JavaCompiler=ToolProvider.getSystemJavaCompiler();
//run(null,null,null,srcDirectory.getPath()+“/Main.java”);
JavaCompiler=ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager=compiler.getStandardFileManager(null,null,null);
Iterable您可以创建一个URLClassLoader来加载新编译的类,也可以查看我编写的库,该库将在内存中编译并默认加载到当前类加载器中
如果您已经生成了代码,则在调试时它会将文件保存到源目录,以便您可以单步执行生成的代码(否则它会在内存中执行所有操作)
您只能以这种方式加载一个类,因此如果需要加载多个版本,我建议您实现一个接口并每次更改类的名称
// this writes the file to disk only when debugging is enabled.
CachedCompiler cc = CompilerUtils.DEBUGGING ?
new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
CompilerUtils.CACHED_COMPILER;
String text = "generated test " + new Date();
Class fooBarTeeClass = cc.loadFromJava("eg.FooBarTee", "package eg;\n" +
'\n' +
"import eg.components.BarImpl;\n" +
"import eg.components.TeeImpl;\n" +
"import eg.components.Foo;\n" +
'\n' +
"public class FooBarTee{\n" +
" public final String name;\n" +
" public final TeeImpl tee;\n" +
" public final BarImpl bar;\n" +
" public final BarImpl copy;\n" +
" public final Foo foo;\n" +
'\n' +
" public FooBarTee(String name) {\n" +
" // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
" System.out.println(\"" + text + "\");\n" +
" this.name = name;\n" +
'\n' +
" tee = new TeeImpl(\"test\");\n" +
'\n' +
" bar = new BarImpl(tee, 55);\n" +
'\n' +
" copy = new BarImpl(tee, 555);\n" +
'\n' +
" // you should see the current date here after synchronisation.\n" +
" foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
" }\n" +
'\n' +
" public void start() {\n" +
" }\n" +
'\n' +
" public void stop() {\n" +
" }\n" +
'\n' +
" public void close() {\n" +
" stop();\n" +
'\n' +
" }\n" +
"}\n");
// add a debug break point here and step into this method.
FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
Foo foo = fooBarTee.foo;
assertNotNull(foo);
assertEquals(text, foo.s);
你可以启动一个URLClassLoader来加载代码和你刚刚编译的类。这在Java中不像某些语言那样容易。我想这正是我需要的,我会查看URLClassLoader并了解它。我非常感谢你的帮助。如果你想继续回答这个问题,我会将它标记为已接受。(我得急急忙忙跑一个小时,但一回来我就跑)