我试图通过使用反射的程序生成、编译和运行java类。我能够成功地生成和编译该文件,但在运行该文件时,得到classnotfoundexception。
package javaapplication2;
/**
*
* @author sachin.maurya
*/
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.lang.ClassLoader;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = "C:\\Users\\sachin.maurya\\Documents\\NetBeansProjects\\JavaApplication2\\src\\javaapplication2\\"+todayClass + ".java";
public static void main (String args[]) throws InterruptedException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":\n\n");
Thread.sleep(2000);
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(\""+todayMillis+"\");");
aWriter.write(" }}\n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = { new String(todaySource)};
ByteArrayOutputStream baos= new ByteArrayOutputStream();
new sun.tools.javac.Main(baos,source[0]).compile(source);
return (baos.toString().indexOf("error")==-1);
}
public void runIt() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
跑步:
Running z_1462876181460:
1462874906040
javaapplication2.Runner.runner()
java.lang.ClassNotFoundException: z_1462876181460
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at javaapplication2.Runner.runner(Runner.java:24)
at javaapplication2.MakeTodayClass.runIt(MakeTodayClass.java:70)
at javaapplication2.MakeTodayClass.main(MakeTodayClass.java:28)
BUILD SUCCESSFUL (total time: 2 seconds)
当我硬编码'todayclass'值的值时,即之前生成的某个类文件runit()函数工作正常,所以这似乎是由于classloader的工作流造成的,但我不确定。是否可以运行生成的.class文件?
1条答案
按热度按时间ars1skjm1#
问题是
ClassLoader
在类路径中找不到创建的类。最简单的修复方法是以不同的方式定义文件位置,比如(假设
bin
在类路径中):