public interface Reloadable { public void test(); }
public class DynaLoad implements Reloadable { static { System.out.println("Loading"); } public void test() { System.out.println("Test..."); } }
import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader;public class ReloadTest { public Reloadable obj; public void reload() { // Get the directory (URL) of the reloadable class URL[] urls = null; try { // Convert the file object to a URL File dir = new File("." + File.separator + "dir"); // "./dir" URL url = dir.toURL(); urls = new URL[]{url}; } catch (MalformedURLException e) { e.printStackTrace(); } try { // Create a new class loader with the directory ClassLoader cl = new URLClassLoader(urls); // Load in the class Class cls = cl.loadClass("DynaLoad"); // Create a new instance of the new class obj = (Reloadable) cls.newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { ReloadTest test = new ReloadTest(); while (true) { test.reload(); test.obj.test(); System.in.read(); System.gc(); } } }
因为如果是同一个类, 无论再怎么加载,那些 class 对象都是完完全全
所以没有这个必要, JVM 不会这样做
你这样做是出于什么目的,或者想达到什么效果呢?------------------------------------------------
lefteye(入门者) JVM加载CLASS后,能不能强制重新加载Class.而不使用已加载的CLASS实例化对象。
就编一程序,程序开启后用来产生对象实例达到运行的目的。心想JAVA内开个线程总比进程快。
可程序开启后。产生对象实例、修改、编译。再次产生实例。(问题是无法用重新编译后的CLASS产生实例。)
重新编译 class 文件后必须重新运行
这就好像重新编译 exe 文件后必须重新运行一样
java不提供动态编译功能
你自己要想实现这个功能就好比想要实现一个小型的操作系统
操作系统所做的大概就是你想要的功能
自己可以体味一下
编辑classB.
编译classB.
用运行中的classA产生classB的实例。
再次编辑classB.
编译classB.
用运行中的classA产生classB的实例。
整个过程中classA一直运行。(问题是:classA不再次读取类classB用来产生实例.)
public void test();
}
static {
System.out.println("Loading");
} public void test() {
System.out.println("Test...");
}
}
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;public class ReloadTest {
public Reloadable obj; public void reload() {
// Get the directory (URL) of the reloadable class
URL[] urls = null; try {
// Convert the file object to a URL
File dir = new File("." + File.separator + "dir");
// "./dir"
URL url = dir.toURL();
urls = new URL[]{url};
} catch (MalformedURLException e) {
e.printStackTrace();
} try {
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls); // Load in the class
Class cls = cl.loadClass("DynaLoad"); // Create a new instance of the new class
obj = (Reloadable) cls.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
ReloadTest test = new ReloadTest(); while (true) {
test.reload();
test.obj.test();
System.in.read();
System.gc();
}
}
}
DynaLoad.class 放在前面两个文件所在目录的子目录 /dir/ 下面
运行 ReloadTest 测试
这样做可以达到你想要的效果