[RESOLVIDO] RMI ClassNotFoundException: HelloImpl_Stub

4 respostas
vi-gb

Estou seguindo o exemplo da sun sobre RMI http://java.sun.com/j2se/1.4.2/docs/guide/rmi/getstart.doc.html mas nao consigo fazer a applet baixar o stub.
Quando é local, funciona normal mas pela applet nao vai fica dando este erro:

HelloApplet exception: error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: HelloImpl_Stub java.rmi.UnmarshalException: error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: HelloImpl_Stub at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) at examples.hello.HelloApplet1.init(HelloApplet1.java:31) at sun.applet.AppletPanel.run(AppletPanel.java:424) at java.lang.Thread.run(Thread.java:619) Caused by: java.lang.ClassNotFoundException: HelloImpl_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:307) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 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:1575) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) ... 4 more

Alguem sabe como fazer para funcionar?

4 Respostas

CarvalR2

Este HelloImpl_Stub implementa alguma interface certo ? por exemplo, Hello_Stub

Ta parecendo que a interface, Hello_Stub deveria estar no classpath de seu applet

vi-gb

Obrigada por responder.

O Hello é a interface e HelloImpl a implementacao.

Estas sao minhas classes:

import java.rmi.Remote; 
import java.rmi.RemoteException; 

public interface Hello extends Remote { 
    public String sayHello() throws RemoteException; 
}
import java.applet.Applet; 
import java.awt.Graphics; 
import java.rmi.Naming; 
import java.rmi.RemoteException; 

public class HelloApplet extends Applet { 

    String message = "blank"; 
  
    // "obj" is the identifier that we'll use to refer 
    // to the remote object that implements the "Hello" 
    // interface 
    Hello obj = null; 

    public void init() { 
	try { 

	    obj = (Hello)Naming.lookup("//" + 
			 getCodeBase().getHost() + "/HelloServer"); 
	    message = obj.sayHello(); 
	} catch (Exception e) { 
	    System.out.println("HelloApplet exception: " + 
				    e.getMessage()); 
	    e.printStackTrace(); 
	} 
    } 

    public void paint(Graphics g) { 
	g.drawString(message, 25, 50); 
    } 
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject
    implements Hello {

    public HelloImpl() throws RemoteException {
        super();
    }

    public String sayHello() throws RemoteException{
        return  "Hello World!";
    }


}
import java.rmi.Naming;

public class HelloServer {
public HelloServer() { 

       try { 
	    Hello obj = new HelloImpl(); 
	    // Bind this object instance to the name "HelloServer" 
	    Naming.rebind("rmi://localhost:1099/HelloServer", obj); 
	    System.out.println("HelloServer bound in registry"); 
        } catch (Exception e) { 
	    System.out.println("HelloImpl err: " + e.getMessage()); 
	    e.printStackTrace(); 
        } 
    } 

public static void main(String[] args) {
		new HelloServer();
	}
}

depois compilo tudo e gero o stub e starto o rmi e o server, se fizer o teste local funciona mas a applet nao pega, como faço pra ela enxergar onde esta o meu stub?

tentei startar o server assim pois segundo o tutorial, é pelo codebase que a applet tenta baixar o stub e nao adiantou
java -Djava.rmi.server.codebase=http://localhost:8080/RMIExample/stubTeste/ HelloServer

Oque estou fazendo de errado?

vi-gb

Consegui fazer funcionar sem gerar o stub atraves deste exemplo:

Codigo para localizar o servico na applet

try { Registry registry = LocateRegistry.getRegistry(host, PORTA); IRemoteService serv = (IRemoteService) registry.lookup(IRemoteService.serviceName); } catch (Exception e) { e.printStackTrace(); }

Codigo para iniciar o rmi e registrar o servico no servidor, coloquei este codigo em uma classe que era acessada logo que o servidor subia

IRemoteService service = new RemoteServiceImpl();
IRemoteService stub =
						(IRemoteService) UnicastRemoteObject.exportObject(service, 0);
					Registry registry = LocateRegistry.createRegistry(PORTA);
					registry.rebind(IRemoteService.serviceName, stub);

Classes utilizadas

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IRemoteService extends Remote { 

    public final String serviceName = "MyRemoteService"; 
    public abstract void startDoing() throws RemoteException; 

    public abstract void stopDoing() throws RemoteException; 
}


import java.rmi.RemoteException;

public class RemoteServiceImpl implements IRemoteService { 
    public RemoteServiceImpl(){ 
        super(); 
    } 
    public void startDoing() throws RemoteException { 
    	System.out.println("I'm starting");
    } 
    public void stopDoing() throws RemoteException { 
    	System.out.println("I'm stoping");
    }
}

Vlw Romilson.

CarvalR2

Cara, meus parabéns viu por chegar na solução.
Minha ajuda foi realmente pouca, pois há muito não utilizo RMI diretamente.

Mas meus parabéns por conseguir chegar na solução!

Criado 4 de junho de 2010
Ultima resposta 4 de jun. de 2010
Respostas 4
Participantes 2