我正在使用动态执行,但它告诉我找不到该类,尽管我仔细检查了路径并且它是正确的
这是我正在使用的方法
public static void runIt(String fileToCompile) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, SecurityException, NoSuchMethodException
{System.out.println("Entered runIt()");
String r2="";
File file=File("/Users/apple/Documents/Documents/workspace/UserTesting/src/two.java");
System.out.println("The path of the file is "+fileToCompile);
System.out.println("HERE 1");
try
{
// Convert File to a URL
URL url = file.toURL(); // file:/classes/demo
URL[] urls = new URL[] { url };
System.out.println("HERE 2");
// Create a new class loader with the directory
ClassLoader loader = new URLClassLoader(urls);
System.out.println("HERE 3");
System.out.println("HERE 4");
Class<?> thisClass=null;
try{
thisClass = classLoader.loadClass("two");
}
catch(ClassNotFoundException e){
System.out.println("Class not found");
}
System.out.println("HERE 5");
Object newClassAInstance = thisClass.newInstance();
System.out.println("HERE 6");
Class params[] = new Class[1];
params[0]=String[].class;
Object paramsObj[] = {};
String m=null;
Object instance = thisClass.newInstance();
System.out.println("HERE 7");
Method thisMethod = thisClass.getDeclaredMethod("main", params);
System.out.println("HERE 8");
String methodParameter = "a quick brown fox";
// run the testAdd() method on the instance:
System.out.println((String)thisMethod.invoke(instance,(Object)m));
}
catch (MalformedURLException e)
{
}
}
这打印
这里 1
这里 2
这里 3
在这里 4
找不到类
这里 5
方法中是否缺少任何东西