求解 java动态编译找不到类,往各位大虾指导

2024-12-01 14:43:45
推荐回答(1个)
回答(1):

参照网上其他童鞋的例子与介绍,修改了一下:
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

/**
* 动态编译Java 源文件
*
* @author shenshouer
*
*/
public class CompilerTest {

public static void main(String[] args) throws Exception {
String source = "public class Main { " + "public static void main(String[]args) {"
+ "System.out.println(\"Hello World!\");" + "} " + "}";

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main",
source);
Iterable fileObjects = Arrays.asList(sourceObject);

// 获取编译类根路径,不然会报找不到类的错误
String path = Class.class.getClass().getResource("/").getPath();
Iterable< String> options = Arrays.asList("-d", path);

// 增加options参数
// CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects);

boolean result = task.call();

if (result) {
System.out.println("编译成功。");

ClassLoader loader = CompilerTest.class.getClassLoader();
try {
Class clazz = loader.loadClass("Main");
Method method = clazz.getMethod("main", String[].class);
// 修改调用参数,不然会报参数个数不对
// Object value = method.invoke(null, new Object[] {});
Object value = method.invoke(null, new Object[] {new String[]{}});
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}

static class StringSourceJavaObject extends SimpleJavaFileObject {

private String content = null;

public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
Kind.SOURCE);
this.content = content;
}

public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
}
}