我正在尝试使用反射通过程序生成,编译和运行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文件?
答案 0 :(得分:0)
问题是public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
// Brief: Handle data received event
{
// Read the data
// ~~~~~~~~~~~~~
// Check for unsubscribe
// ~~~~~~~~~~~~~~~~~~~~~
if (bStopDataRequested)
{
serialPort1.DataReceived -= DataReceivedHandler; // Unsubscribe to DataReceived event
// Only then close the port
}
}
无法在类路径中找到创建的类。
最简单的解决方法是以不同的方式定义文件位置,或许类似(假设ClassLoader
在您的类路径中):
bin