2011-12-31 14 views
5

Seguí el ejemplo en los libros, pero estoy recibiendo muchos errores.Head First Java - problemas de RMI

Ejecuté el rmiregistry.

Luego hice.

java MyremoteImpl 

que me dio el siguiente error:

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 me da el siguiente error:

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) 

Aquí está mi código 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(); 

} 
    } 
} 

Código 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(); 

    } 
} 

} 

Código MyRemote:

import java.rmi.*; 

public interface MyRemote extends Remote { 

public String sayHello() throws RemoteException; 

} 
+0

¿Cómo se declara MyRemote? – Tudor

+0

No estoy seguro de si esto tiene algo que ver con la excepción que recibe, pero nunca debe declarar clases en el paquete predeterminado. –

+0

Utilicé mis propios paquetes, obtuve el mismo error, entonces utilicé paquetes predeterminados y todavía tuve el mismo problema –

Respuesta

0

El "código dinámico de la descarga usando RMI" sección documenta esta cuestión, que se puede encontraron @http://docs.oracle.com/javase/1.4.2/docs/guide/rmi/codebase.html

Y para citar:

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

He intentado todo con rmi: // localhost/RemoteHello "pero todavía no tengo suerte –

+0

@ YK-47 Obviamente usted * no * "intenté todo" en absoluto, de lo contrario lo haría funcionar. – EJP

+0

No hay evidencia aquí de que esté usando la función de base de código en absoluto, y menos mal. -1 – EJP

1

El registro no tiene esa clase en su classpath. O tiene que ejecutar el Registro de tal manera que lo haga, p. LocateRegistry.createRegistry(), o tiene que usar la función de base de código de RMI para que el registro y el cliente sepan dónde obtener las clases que faltan.

2

Tengo este error antes y descubrió que tenía que ejecutar "rmiregistry" en la carpeta donde "MyRemoteImpl_Stub.class" es. ¡Buena suerte!

1

Prueba esto en principales (args String []) de servidor:

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