我在String中编写了一个简单的Java代码,想在程序运行时动态执行,以下是我的代码:
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompliedClass {
public static void main(String[] args) {
String source = ""
+"class Solution{"
+"public int add(){"
+"return 1+1;"
+"}"
+"}";
//File root = new File("");
File sourceFile = new File("./src/Solution.java");
try {
Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sourceFile.getPath());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
System.out.println("JDK required (running inside of JRE)");
}else{
System.out.println("you got it!");
}
int compilationResult = compiler.run(null, null, null,sourceFile.getPath());
if(compilationResult == 0){
System.out.println("Compilation is successful");
}else{
System.out.println("Compilation Failed");
}
try{
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { sourceFile.toURI().toURL() });
Class<?> cls = Class.forName("Solution" , true, classLoader);
Object instance = cls.newInstance();
Method method = cls.getDeclaredMethod("add", null);
System.out.println(method.invoke(instance, null));
}catch(Exception e){
System.out.println("something wrong");
}
}
}
上面的代码的问题是当我第一次执行时,我无法获得结果,似乎下面的代码有一个例外:
Object instance = cls.newInstance();
然后我第二次执行,它的功能很好,所以结论是当我第一次运行时,找不到Solution类,这导致下面的异常
java.lang.ClassNotFoundException: Solution
有人可以帮我解决这个问题吗?
解决方法:
1)您的类应具有public修饰符,否则,如果未在同一包中声明,CompliedClass将无法访问它.在这里它可以像默认包一样工作,但是不建议这样做.
2)您的错误在这里:
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { sourceFile.toURI().toURL() });
这里的sourceFile是指解决方案java类文件.
而是应根据URLClassLoader的javadoc java.net.URLClassLoader.newInstance(URL [] urls)(即url参数)引用包含Solution Java类文件的文件夹的url.
是指用于搜索类和资源的URL.
您可以尝试使用以下两个经过解释的修改来编写此代码:
public class CompliedClass {
public static void main(String[] args) {
String source = "public class Solution{" + "public int add(){" + "return 1+1;" + "}" + "}";
File folder = new File("./src");
File sourceFile = new File(folder, "Solution.java");
try {
Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sourceFile.getPath());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
System.out.println("JDK required (running inside of JRE)");
} else {
System.out.println("you got it!");
}
int compilationResult = compiler.run(null, null, null, sourceFile.getPath());
if (compilationResult == 0) {
System.out.println("Compilation is successful");
} else {
System.out.println("Compilation Failed");
}
try {
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {folder.toURI().toURL() });
Class<?> cls = Class.forName("Solution", true, classLoader);
Object instance = cls.newInstance();
Method method = cls.getDeclaredMethod("add", null);
System.out.println(method.invoke(instance, null));
} catch (Exception e) {
e.printStackTrace();
System.out.println("something wrong");
}
}
}
输出:
.\src\Solution.java
you got it!
Compilation is successful
2