本文纯粹就是将一篇英文文章的一个段落翻译一下,并将例子跑通。
原文出处:http://java.sun.com/developer/TechTips/2000/tt1027.html
我的目录结构是
d://hotdeploy//Client.java
d://hotdeploy//ServerItf.java
d://hotdeploy//server//ServerImpl.java
文件内容依次为:
//file Client.java
import java.net.URL;
import java.net.URLClassLoader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Client {
static ClassLoader cl;
static ServerItf server;
public static void loadNewVersionOfServer() throws Exception {
URL[] serverURLs = new URL[] { new URL("file://d:/hotdeploy/server/") };
cl = new URLClassLoader(serverURLs);
server = (ServerItf) cl.loadClass("ServerImpl").newInstance();
}
public static void test() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
loadNewVersionOfServer();
while (true) {
System.out.print("Enter QUOTE, RELOAD, GC, or QUIT: ");
String cmdRead = br.readLine();
String cmd = cmdRead.toUpperCase();
if (cmd.equals("QUIT")) {
return;
} else if (cmd.equals("QUOTE")) {
System.out.println(server.getQuote());
} else if (cmd.equals("RELOAD")) {
loadNewVersionOfServer();
} else if (cmd.equals("GC")) {
System.gc();
System.runFinalization();
}
}
}
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------
public interface ServerItf {
public String getQuote();
}
-------------------------------------------------------------------------
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "i love you";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
运行的命令依次为:
D:/hotdeploy>javac Client.java
D:/hotdeploy>javac ServerItf.java
D:/hotdeploy>javac -cp d:/hotdeploy d:/hotdeploy/server/ServerImpl.java
D:/hotdeploy>java Client
ServerImpl class 1641745 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
i love you
Enter QUOTE, RELOAD, GC, or QUIT:
-------------------------------------------------------------------------
编辑ServerImpl.java为:
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "you love me";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
打开另外一个dos窗口,运行javac -cp d:/hotdeploy d:/hotdeploy/server/ServerImpl.java
-------------------------------------------------------------------------
回到原先的doc窗口,依次运行
Enter QUOTE, RELOAD, GC, or QUIT: reload
ServerImpl class 12677476 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
you love me
Enter QUOTE, RELOAD, GC, or QUIT:
可以看到效果。