Java Class Hot Deploy
2004-09-22 15:36
363 查看
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: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.println("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();
System.gc();//要求虚拟机回收旧类装载器
System.runFinalization();
}
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();
}
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");
}
};
public class ServerImpl implements ServerItf
{
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote(){ return " A rolling stong gathers no moss";}
}
};
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: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.println("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();
System.gc();//要求虚拟机回收旧类装载器
System.runFinalization();
}
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();
}
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");
}
};
public class ServerImpl implements ServerItf
{
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote(){ return " A rolling stong gathers no moss";}
}
};
相关文章推荐
- Android的4种文件类型Java,class,dex,apk
- java.lang.UnsupportedClassVersionError !
- java.lang.ClassNotFoundException: org.apache.commons.collections.FastHashMap
- Java ClassLoader基础及加载不同依赖 Jar 中的公共类
- java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilte
- java.lang.ClassNotFoundException: org.apache.http.util.Args
- 在写junit test 的时候出现的java.lang.UnsupportedClassVersionError问题
- java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFi
- SSH+ maven整合:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- java-静态同步函数的锁是Class对象
- 1.7号 java.lang.ClassNotFoundException: Didn't find class "tv.pptv.MainActivity
- There is no getter for property named 'userIds' in 'class java.lang.String'
- Hibernate java.lang.ClassCastException ERROR解决方法
- Java 获取当前文件.class 的相对或绝对路径
- 解决java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoader
- Windows下 JAVA_HOME PATH CLASSPATH 三个变量的配置
- 调用百度地图API时出现java.lang.UnsatisfiedLinkError: initClass
- 最简单的框架(配制文件config.properties)文件内容:className=java.util.ArrayList
- 运行java的class文件方法详解
- java.lang.ClassCastException: 。。。$LayoutParams cannot be cast to android.widget.LinearLayout$Layout