Head First Java - RMI问题

时间:2011-12-31 17:03:35

标签: java rmi

我按照书上的例子,但我遇到了很多错误。

我跑了rmiregistry。

然后我完成了。

java MyremoteImpl

给了我以下错误:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: MyRemoteImpl_Stub
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
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.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at MyRemoteImpl.main(MyRemoteImpl.java:25)
    Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: MyRemoteImpl_Stub
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
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)
    Caused by: java.lang.ClassNotFoundException: MyRemoteImpl_Stub
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:434)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:165)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:620)
at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:247)
at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:197)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1574)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)

MyRemoteClient给出了以下错误:

java.rmi.NotBoundException: RemoteHello
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:106)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
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.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:84)
at MyRemoteClient.go(MyRemoteClient.java:11)
at MyRemoteClient.main(MyRemoteClient.java:6)

这是我的MyRemoteImpl代码:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote  {

@Override
public String sayHello(){
    return "server says, 'Hey'";    
}

public MyRemoteImpl() throws RemoteException{ 

}

    public static void main (String [] args){

try{
    MyRemote service = new MyRemoteImpl();
    Naming.rebind("RemoteHello", service);
} catch (Exception ex) { ex.printStackTrace();

} 
    }
}

MyRemoteClient代码:

import java.rmi.*;


public class MyRemoteClient {
public static void main(String [] args){
    new MyRemoteClient().go();
}

public void go(){
    try {
        MyRemote service = (MyRemote)   Naming.lookup("rmi://127.0.0.1/RemoteHello");
        String s = service.sayHello();

        System.out.println(s);
    }catch(Exception ex){
        ex.printStackTrace();

    }
}

}

MyRemote代码:

import java.rmi.*;

public interface MyRemote extends Remote {

public String sayHello() throws RemoteException;

}

4 个答案:

答案 0 :(得分:2)

之前我遇到此错误,发现我必须在“MyRemoteImpl_Stub.class”所在的文件夹中运行“rmiregistry”。祝你好运!

答案 1 :(得分:1)

注册表在其类路径上没有该类。您必须以这样的方式运行注册表,例如, LocateRegistry.createRegistry(),或者您必须使用RMI代码库功能,以便注册表和客户端知道从哪里获取缺少的类。

答案 2 :(得分:1)

在服务器的main(String [] args)中试试这个:

try{
    MyRemote service = new MyRemoteImpl();
    //Naming.rebind("RemoteHello", service);
    Registry registry = LocaleRegistry.getRegistry();
    registry.bind("RemoteHello", service);
} catch (Exception ex) {
    ex.printStackTrace();
}

答案 3 :(得分:0)

“使用RMI下载动态代码”部分记录了这个问题,可以是 找到@ http://docs.oracle.com/javase/1.4.2/docs/guide/rmi/codebase.html

引用:

  

6.1如果您在运行RMI服务器时遇到问题

     

您可能遇到的第一个问题是在尝试将远程对象绑定或重新绑定到注册表中的名称时收到ClassNotFoundException。此异常通常是由于代码库属性格式错误导致注册表无法找到远程对象的存根或存根所需的其他类。

     

重要的是要注意远程对象的存根实现与远程对象本身相同的所有接口,因此这些接口以及声明为方法参数或返回值的任何其他自定义类也必须可从下载指定的代码库。

     

最常见的情况是,由于省略了属性的URL值的尾部斜杠而抛出此异常。其他原因包括:属性的值不是URL; URL中指定的类的路径不正确或拼写错误;存根类或任何其他必要的类都不能从指定的URL中获得。

     

在这种情况下您可能遇到的例外情况如下:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
    java.lang.ClassNotFoundException: examples.callback.MessageReceiverImpl_Stub
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
    java.lang.ClassNotFoundException: examples.callback.MessageReceiverImpl_Stub
java.lang.ClassNotFoundException: examples.callback.MessageReceiverImpl_Stub
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Compiled Code)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Compiled Code)
    at sun.rmi.server.UnicastRef.invoke(Compiled Code)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Compiled Code)
    at java.rmi.Naming.rebind(Compiled Code)
    at examples.callback.MessageReceiverImpl.main(Compiled Code)
RemoteException occurred in server thread; nested exception is:
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: examples.callback.MessageReceiverImpl_Stub