问题描述
我把一个RMI的CLIENT调用类放在SERVER的工程里执行可以正常调用,但是如果把它拷到一个新项目中却无法执行,代码如下://服务器代码packageserver;importjava.rmi.Remote;importjava.rmi.RemoteException;publicinterfaceSayHelloextendsRemote{//需要从Remote继承publicStringsayHello(Stringinfo)throwsRemoteException;//需要抛出remote异常n;}packageserver;importjava.rmi.RemoteException;importjava.rmi.server.UnicastRemoteObject;publicclassSayHelloImplextendsUnicastRemoteObjectimplementsSayHello{protectedSayHelloImpl()throwsRemoteException{super();//TODOAuto-generatedconstructorstub}publicStringsayHello(Stringinfo)throwsRemoteException{return"hello="+info;}}packageserver;importjava.rmi.*;importjava.net.*;publicclassSayRmiServer{publicSayRmiServer(){}publicstaticvoidmain(Stringargs[])throwsRemoteException,MalformedURLException{SayHelloImpladd=newSayHelloImpl();Naming.rebind("RMI_Test_2",add);}}//客户端代码,接口文件就不重复拷了packageclient;importjava.net.MalformedURLException;importjava.rmi.Naming;importjava.rmi.NotBoundException;importjava.rmi.RemoteException;importserver.SayHello;publicclassSayRmiClient{publicSayRmiClient(){}publicstaticvoidmain(Stringargs[])throwsRemoteException,MalformedURLException,NotBoundException{Stringurl="rmi://110.64.89.140"+"/RMI_Test_2";SayHelloadd=(SayHello)Naming.lookup(url);Stringresult=add.sayHello("hubin");System.out.println(result);}}
执行后抛出如下异常:Exceptioninthread"main"java.rmi.UnmarshalException:errorunmarshallingreturn;nestedexceptionis:java.lang.ClassNotFoundException:server.SayHello(nosecuritymanager:RMIclassloaderdisabled)atsun.rmi.registry.RegistryImpl_Stub.lookup(UnknownSource)atjava.rmi.Naming.lookup(UnknownSource)atclient.SayRmiClient.main(SayRmiClient.java:22)Causedby:java.lang.ClassNotFoundException:server.SayHello(nosecuritymanager:RMIclassloaderdisabled)atsun.rmi.server.LoaderHandler.loadProxyClass(UnknownSource)atjava.rmi.server.RMIClassLoader$2.loadProxyClass(UnknownSource)atjava.rmi.server.RMIClassLoader.loadProxyClass(UnknownSource)atsun.rmi.server.MarshalInputStream.resolveProxyClass(UnknownSource)atjava.io.ObjectInputStream.readProxyDesc(UnknownSource)atjava.io.ObjectInputStream.readClassDesc(UnknownSource)atjava.io.ObjectInputStream.readOrdinaryObject(UnknownSource)atjava.io.ObjectInputStream.readObject0(UnknownSource)atjava.io.ObjectInputStream.readObject(UnknownSource)...3more
解决方案
解决方案二:
server.SayHello要拷贝到客户端,且要实现系列化implementsSerializable
解决方案三:
引用1楼nanman的回复:
server.SayHello要拷贝到客户端,且要实现系列化implementsSerializable
+1另外你可以把服务端通信需要用到的类生成jar文件给客户端就可以了。
解决方案四:
引用1楼nanman的回复:
server.SayHello要拷贝到客户端,且要实现系列化implementsSerializable
+1