Dynamic Proxy 在 Java RMI 中的应用 这篇文章google一搜,尽是转载
  
        请教各位RMI达人:
 这篇文章中的实例是否可实现,
在simpleserver中的
    URLClassLoader cl = new DynamicClassLoader(new URL[] { new
                      File("demo").toURL() });
demo文件内容是啥呢?
我这个地方运行时报错如下C:\code\proxy>start java -Djava.rmi.server.codebase=file:/c:/code/ SimpleServerC:\code\proxy>java -Djava.rmi.server.codebase=file:/c:/code/ SimpleServer
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleServer
Caused by: java.lang.ClassNotFoundException: SimpleServer
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

解决方案 »

  1.   

    Caused by: java.lang.ClassNotFoundException: SimpleServer 
    报错都给你说了,没有找到该类!
      

  2.   

    根据3楼进行的修正,但这回更奇怪,跟到这个simpleserver里面debug,就是到 UnicastRemoteObject.exportObject(server);
    报错。
    server一直是有内容的,高手指点啊[code=Java]
    import java.lang.reflect.Proxy;
    import java.io.*;
    import java.net.*;
    import java.rmi.*;
    import java.rmi.server.*;
    import java.rmi.registry.*;public class SimpleServer
    {
        public static void main(String[] args)
        {
            try {
                // Create remote object
                URLClassLoader cl = new DynamicClassLoader(new URL[] { new
                          File("demo").toURL() });            Remote server = (Remote)cl.loadClass("SmallWorldImpl").newInstance();            // it should be DynamicRemoteStub
                System.out.println("StubObject is: " +
                    RemoteObject.toStub((RemoteObject)server).getClass());            // add log Proxy to the remote object
                server = (Remote)Proxy.newProxyInstance(server.getClass().getClassLoader(),
                                                        new Class[] { SmallWorld.class },
                                                        new LogProxy(server));            // add performance Proxy to the remote object
                server = (Remote)Proxy.newProxyInstance(server.getClass().getClassLoader(),
                                                        new Class[] { SmallWorld.class },
                                                        new PerformanceProxy(server));            UnicastRemoteObject.exportObject(server);
                Registry reg = LocateRegistry.createRegistry(1099);
                reg.bind(SmallWorld.NAME, server);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    [code]C:\code\proxy>java -Djava.rmi.server.codebase=file:/c:/code/proxy/ SimpleServerStubObject is: class SmallWorldImpl_Stub
    java.rmi.StubNotFoundException: Stub class not found: $Proxy0_Stub; nested excep
    tion is:
            java.lang.ClassNotFoundException: $Proxy0_Stub
            at sun.rmi.server.Util.createStub(Unknown Source)
            at sun.rmi.server.Util.createProxy(Unknown Source)
            at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
            at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
            at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
            at SimpleServer.main(SimpleServer.java:33)
    Caused by: java.lang.ClassNotFoundException: $Proxy0_Stub
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            ... 6 more
      

  3.   

    所在目录内容如下
     C:\code\proxy 的目录2008-06-20  08:28    <DIR>          .
    2008-06-20  08:28    <DIR>          ..
    2008-06-20  09:54             5,080 proxy.zip
    2008-06-20  09:52    <DIR>          client
    2008-06-20  08:11               169 client.policy
    2008-06-20  09:55             1,763 SmallWorldImpl_Stub.class
    2008-06-20  12:02            19,322 testRandom.zip
    2008-06-18  11:09             1,561 testRandom.class
    2008-06-20  11:08             1,300 DynamicClassLoader.class
    2008-06-20  09:35             1,169 DynamicRemoteStub.class
    2008-06-20  09:30             1,250 DynamicStubHandler.class
    2008-06-20  09:44               966 LogProxy.class
    2008-06-20  09:49               998 PerformanceProxy.class
    2008-06-20  09:49             2,281 SimpleServer.class
    2008-06-20  09:25               280 SmallWorld.class
    2008-06-20  09:25               831 SmallWorldImpl.class
    2008-06-20  11:08             1,213 DynamicClassLoader.java
    2008-06-20  09:35             1,038 DynamicRemoteStub.java
    2008-06-20  09:30               903 DynamicStubHandler.java
    2008-06-20  09:44               575 LogProxy.java
    2008-06-20  09:49               597 PerformanceProxy.java
    2008-06-20  09:49             1,543 SimpleServer.java
    2008-06-20  09:25               225 SmallWorld.java
    2008-06-20  09:25               465 SmallWorldImpl.java
    2008-06-18  11:09               787 testRandom.java
                  22 个文件         44,316 字节
                   3 个目录  4,860,551,168 可用字节
      

  4.   

    我的这两篇文章可能会给你启示,你的类加载器找不到需要的类。
    http://blog.csdn.net/sunyujia/archive/2008/06/08/2524105.aspx
    http://blog.csdn.net/sunyujia/archive/2008/06/01/2500684.aspx
      

  5.   

    真是奇怪了,SmallWorldImpl_Stub.class明明就在目录file:/c:/code/proxy/ 下面,莫非变身之后他要的不是这个stub?
      

  6.   

              URLClassLoader cl = new DynamicClassLoader(new URL[] { new 
                          File("demo").toURL() }); 
    你是定义了,但是好像我没有看到你那里有用,下面的两个地方都应该用你定义的。
      

  7.   

    It is used here:(Remote server = (Remote)    cl     .loadClass("SmallWorldImpl").newInstance(); 
      

  8.   

     URLClassLoader cl = new DynamicClassLoader(new URL[] { new 
                          File("demo").toURL() }); 
      

  9.   

    what is wrong with this code?