Reputation: 484
I have used
Naming.rebind("rmi://localhost:1099/RmiServer", c);
where c is a remote object. And
XYZ robj=new XYZ();
ABC stub = (ABC) UnicastRemoteObject.exportObject(robj, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("RmiServer", stub);
where stub is the remote object.
The first case is working fine if the rmiregistry is running already,but the later is throwing ServerException that ClassNotFoundException
is thrown for ABC class. Where am i going wrong?
Also how are Naming and Registry different
in the sense that here Naming is binding to local host whereas later is getting the corresponding port and address dynamically and then binding the object to it.
Upvotes: 1
Views: 6740
Reputation: 311054
The first case is working fine if the rmiregistry is running already,but the later is throwing ServerException that ClassNotFoundException is thrown for ABC class. Where am i going wrong?
You have that back to front. If you use the separate rmiregistry
you will get ClassNotFoundExceptions
if the remote interfaces and stubs etc aren't on the rmiregistry's CLASSPATH. If you start the Registry in-process via LocateRegistry.createRegistry()
that cannot happen, otherwise the export would have failed prior to the bind()
.
Also how are Naming and Registry different in the sense that here Naming is binding to local host whereas later is getting the corresponding port and address dynamically and then binding the object to it.
Naming
provides a set of static methods that interface to the Registry without you having to call LocateRegistry.getRegistry()
. The API is pretty similar but the name strings are different: they are URL-based.
With the Naming
class, all name strings are RMI URLs, e.g. rmi://localhost:1099/. This is true of all methods: bind(), rebind(), unbind(), lookup(), and list().
With the Registry
interface, all name strings are just strings, e.g. <server-name>.
So if you're using both classes in different parts of the code, you need to keep this straight to ensure interoperability. For example it is possible to use Naming-style URLs in the Registry interface, they are just treated as strings, but then the results of Naming.list() would be something like rmi://localhost:1099/rmi://localhost:1099/<server-name>. So don't do that.
In both cases the names you get back from list() are the same kinds of names you would have supplied to one of the other methods.
Upvotes: 3