我正在开发一个运行当前活动文件的 Eclipse 插件。我正在使用这种方法
public static void runIt(String fileToCompile,String packageName) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, SecurityException, NoSuchMethodException
{
File file = new File(fileToCompile);
try
{
// Convert File to a URL
URL url = file.toURL(); // file:/classes/demo
URL[] urls = new URL[] { url };
// Create a new class loader with the directory
ClassLoader loader = new URLClassLoader(urls);
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
Class<?> thisClass = classLoader.loadClass("NewFile");
Object newClassAInstance = thisClass.newInstance();
Class params[] = new Class[1];
params[0]=String[].class;
Object paramsObj[] = {};
String m=null;
Object instance = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("main", params);
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)
{
}
}
但是当我“启动 Eclipse 应用程序”[在另一个 Eclipse 窗口中运行插件]时它可以工作,但是当我在 Eclipse 中安装插件时它不再工作了。问题出在这一行
类 thisClass = classLoader.loadClass("NewFile"); 它找不到要执行的类