0

I am new to java RMI and was trying to run sample java rmi program. But I am getting java.rmi.notboundexception . Below is my code.

server code

server.java 

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {

public Server() {}

public String sayHello() {
return "Hello, world!";
}

public static void main(String args[]) {
Hello stub = null;
Registry registry = null;

try {
    Server obj = new Server();
    stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
    registry = LocateRegistry.getRegistry();
    registry.bind("Hello", stub);

    System.err.println("Server ready");
} catch (Exception e) {
    System.err.println("Server exception: " + e.toString());
    e.printStackTrace();

/*  Rewritten as per EJP's remarks 
    try{
// Line 66 , where the error is pointing 
    registry.unbind("Hello");
    registry.bind("Hello",stub);
        System.err.println("Server ready");
    }catch(Exception ee){
    System.err.println("Server exception: " + ee.toString());
        ee.printStackTrace();
    }
}
*/ 


}
} 

client code client.java

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

private Client() {}

public static void main(String[] args) {

String host = (args.length < 1) ? null : args[0];
try {
    Registry registry = LocateRegistry.getRegistry(host);
    Hello stub = (Hello) registry.lookup("Hello");
    String response = stub.sayHello();
    System.out.println("response: " + response);
} catch (Exception e) {
    System.err.println("Client exception: " + e.toString());
    e.printStackTrace();
}
}
}

Interface code

Hello.java

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

public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}

Steps followed

  1. javac Server.java

  2. javac Client.java

  3. Start the Java RMI registry with below command

start rmiregistry

After these, when i run the server with java server command , I am getting below exception. Exception changed from the original post after implementing the changes mentioned by EJP.

C:\Users\hp\Desktop\NUS Modules\Distributed systems\Assignment1\HelloWorld\Hello
World\Server>java Server
Server exception: java.rmi.ServerException: RemoteException occurred in server t
hread; nested exception is:
    java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
    java.lang.ClassNotFoundException: Hello
java.rmi.ServerException: RemoteException occurred in server thread; nested exce
ption is:
    java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
    java.lang.ClassNotFoundException: Hello
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:420
)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
    at sun.rmi.transport.Transport$1.run(Transport.java:178)
    at sun.rmi.transport.Transport$1.run(Transport.java:175)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
57)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:812)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:671)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:276)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
253)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
    at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
    at Server.main(Server.java:61)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
    java.lang.ClassNotFoundException: Hello
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410
)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
    at sun.rmi.transport.Transport$1.run(Transport.java:178)
    at sun.rmi.transport.Transport$1.run(Transport.java:175)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
57)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:812)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:671)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: Hello
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.rmi.server.LoaderHandler$Loader.loadClass(LoaderHandler.java:1207
)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at sun.rmi.server.LoaderHandler.loadClassForName(LoaderHandler.java:1221 
)
    at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:7
31)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:674)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:611)
    at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:6
46)
    at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311
)
    at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStrea
m.java:255)
    at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1559)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
774)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    ... 13 more

Please help to solve this error . Anything wrong in the code or any firewall settings need to be done.

java version - 1.8 operating system - Windows 7

user207421
  • 305,947
  • 44
  • 307
  • 483
Prabhuraj
  • 141
  • 3
  • 17
  • Please indicate which line is # **66** in server.java file. – PM 77-1 Aug 26 '14 at 03:15
  • Ya I have added comment on line 66. It points to the below line registry.unbind("Hello"); – Prabhuraj Aug 26 '14 at 03:44
  • Note that your original code was obscuring the real problem, by assuming it was a bind problem. It wasn't. Don't write code like that. – user207421 Aug 26 '14 at 06:13
  • Another advice: Do not catch a generic `Exception` only. Capture more granular first that the methods you use are likely to throw, going from most specific to more general. – PM 77-1 Aug 26 '14 at 15:22

1 Answers1

0

You're trying to unbind something that isn't there. Get rid of the unbind() call and change the following bind() call to rebind().

But I don't see the point of doing any of that in an exception handler. Instead you should be printing the actual exception, to see what it is, first of all.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I removed the code in the exception handler and instead i am printing the exception now. Could you please check the new exception and help me to resolve this. – Prabhuraj Aug 26 '14 at 06:11