Stub Class not found - only when using port-less exportObject()
Hello,
I have a case ofjava.rmi.StubNotFoundException: Stub class not found that I tracked down to being somewhat related to the way I export my object. I am relieved I have a way to workaround this, but I don't understand why the API seems to work in a way that is not documented, and I am considerting openeing a bug for this.
Here are the details:
My service interface:
publicinterface MyServicesextends Remote{ ...}
My server implementation:
publicclass MyServerimplements MyServices{...}
Note that I am in Java 6, so there is no need to run anrmic compiler on the Remote interface to generate a stub class, and no need for MyServer to extend UnicastRemoteObject.
Eventually here is my main method:
server =new MyServer();
try{
// JDK javadoc says this will "Export the remote object (...) using an anonymous port.
MyServices serverStub =
(MyServices)UnicastRemoteObject.exportObject(server);
}
catch(Exception e){
Log.fatal(THIS_CLASS,"Impossible d'exporter le serveur", e);
}
This latter causes an exception:java.rmi.StubNotFoundException: Stub class not found.
I changed the exportObject call to:
MyServices serverStub =
(MyServices)UnicastRemoteObject.exportObject(server, 0);// or 1024, or whatever value I tried
And then I don't have the exception.
The javadoc forexportObject(Remote obj, int port) says that it "Exports the remote object (...) using the particular supplied port.".
What I don't get is what it has to do with finding the stub class.
Digging depeer (just before sending this post), I noticed that the class-level javadoc for UnicastRemoteObject mentions that:
for "UnicastRemoteObject.exportObject(Remote) method, a stub class (typically pregenerated from the remote object's class using the rmic tool) is loaded and an instance of that stub class is constructed as follows (..)[algorithm skipped].",[whereas] For all other means of exporting (...) aProxy instance is constructed (...)with a RemoteObjectInvocationHandler instance constructed with a RemoteRef."
In other words, a Remote class can only be "dynamically exported" if you specify a port.
To end mixing me up, the Java tutorial explicitly says "It is common to use the value zero [for the port argument of exportObject(..)], which specifies the use of an anonymous port. The actual port will then be chosen at runtime by RMI or the underlying operating system".
Why are dynamic stubs available only when specifying a port, even if the port is 0, which means, "whatever port"?
Do you think this is a bug?

