不知道够不够你用的 Class c = Class.forName("你的类名"); Object i = c.newInstance(); //下面为激活该类的方法 Class[] classes = new Class[0]; Method m = c.getDeclaredMethod(finalizerMethod,classes); Object[] args = new Object[0]; m.invoke(i,args);
// Get the directory (URL) of the reloadable class URL[] urls = null; try { // Convert the file object to a URL File dir = new File(System.getProperty("user.dir") +File.separator+"dir"+File.separator); URL url = dir.toURL(); // file:/c:/almanac1.4/examples/ urls = new URL[]{url}; } catch (MalformedURLException e) { }
try { // Create a new class loader with the directory ClassLoader cl = new URLClassLoader(urls);
// Load in the class Class cls = cl.loadClass("MyReloadableClassImpl");
// Create a new instance of the new class myObj = (MyReloadableClass)cls.newInstance(); } catch (IllegalAccessException e) { } catch (InstantiationException e) { } catch (ClassNotFoundException e) { }
Class c = Class.forName("你的类名");
Object i = c.newInstance();
//下面为激活该类的方法
Class[] classes = new Class[0];
Method m = c.getDeclaredMethod(finalizerMethod,classes);
Object[] args = new Object[0];
m.invoke(i,args);
多谢你的帮助, 不过不行, JVM还是没有重新Load 更改后的Class, 不过还是多谢你
URL[] urls = null;
try {
// Convert the file object to a URL
File dir = new File(System.getProperty("user.dir")
+File.separator+"dir"+File.separator);
URL url = dir.toURL(); // file:/c:/almanac1.4/examples/
urls = new URL[]{url};
} catch (MalformedURLException e) {
}
try {
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// Load in the class
Class cls = cl.loadClass("MyReloadableClassImpl");
// Create a new instance of the new class
myObj = (MyReloadableClass)cls.newInstance();
} catch (IllegalAccessException e) {
} catch (InstantiationException e) {
} catch (ClassNotFoundException e) {
}