2011-12-31 85 views
5

我跟着书上的例子,但我收到了很多错误。头第一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; 

} 
+0

如何MyRemote声明? – Tudor 2011-12-31 17:06:05

+0

不知道这与你得到的异常有什么关系,但你不应该在默认包中声明类。 – 2011-12-31 17:09:43

+0

我没有使用我自己的软件包我得到了同样的错误,所以我然后使用默认软件包,仍然有相同的问题 – 2011-12-31 17:14:55

回答

0

“动态代码中使用RMI下载” 部分的文件这个问题,它可以 发现@http://docs.oracle.com/javase/1.4.2/docs/guide/rmi/codebase.html

再次引述:

6.1 If you encounter a problem running your RMI server

The first problem you might encounter is the receipt of a ClassNotFoundException when attempting to bind or rebind a remote object to a name in the registry. This exception is usually due to a malformed codebase property, resulting in the registry not being able to locate the remote object's stubs or other classes needed by the stub.

It is important to note that the remote object's stub implements all the same interfaces as the remote object itself, so those interfaces, as well as any other custom classes declared as method parameters or return values, must also be available for download from the specified codebase.

Most frequently, this exception is thrown as a result of omitting the trailing slash from the URL value of the property. Other reasons would include: the value of the property is not a URL; the path to the classes specified in the URL is incorrect or misspelled; the stub class or any other necessary classes are not all available from the specified URL.

The exception that you may encounter in such a case would look like this:

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 
+0

我已经尝试了所有与rmi:// localhost/RemoteHello“但仍然没有运气 – 2011-12-31 17:47:01

+0

@ YK-47显然你*没有*'试过所有的东西',否则你就会工作。 – EJP 2012-01-01 08:37:18

+0

没有证据表明他使用代码库功能,更不用说错了。 – EJP 2013-05-05 23:18:30

1

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

2

我之前得到这个错误,发现我不得不在这里“MyRemoteImpl_Stub.class”是文件夹中运行“rmiregistry的”。祝你好运!

1

在服务器的主要(字串[] args)试试这个:

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