最近在学习RMI。
参照官网上写了一个小例子。
我建了3个工程。分别为upper_common,upper_server,upper_client.
其中upper_common中定义了远程服务接口类Upper,upper_server定义了远程接口实现类UpperImpl,upper_client定义了客户端调用类UpperClient。
类代码如下:
//Upper
public interface Upper extends Remote {
public String upperCase(String s) throws RemoteException;
}
//UpperImpl
public class UpperImpl extends UnicastRemoteObject implements Upper {
protected UpperImpl() throws RemoteException {
super();
}
@Override
public String upperCase(String s) throws RemoteException {
return s.toUpperCase();
}
/**
* @param args
*/
public static void main(String[] args) {
try{
Registry r = LocateRegistry.getRegistry();
r.bind("UpperString", new UpperImpl());
System.out.println("RMI server is ready");
}catch(Exception e){
e.printStackTrace();
}
}
}
//UpperClient
public class Upperclient {
/**
* @param args
*/
public static void main(String[] args) {
try{
System.setSecurityManager(new RMISecurityManager());
Upper upper = (Upper) Naming.lookup("rmi://localhost/UpperString");
String results = upper.upperCase("abc");
System.out.println("RMI results:" + results);
}catch(Exception e){
e.printStackTrace();
}
}
}
启动服务器端程序的时候总是有异常:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: access to class loader denied
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:400)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at rmi.server.UpperImpl.main(UpperImpl.java:29)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: access to class loader denied
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:390)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)