Reputation: 111
I've written a simple RMI application, but I cannot bind my Remote object to the RMI registry. The RMI registry is running from a separate cmd window [I'm on Windows 10]. The Remote interface is very simple:
package matram;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteInterface extends Remote {
public String sayHello() throws RemoteException;
}
The implementation is also simple:
package matram;
import java.rmi.RemoteException;
public class Test1 extends java.rmi.server.UnicastRemoteObject implements RemoteInterface {
/**
*
*/
private static final long serialVersionUID = 1L;
public Test1() throws RemoteException {
super();
}
@Override
public String sayHello() throws RemoteException {
{ return "Hello world!"; }
}
}
Here's the server, which contains the main method:
package matram;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.Naming;
public class RemoteDCmain {
public static Test1 dc;
public static void main(String[] args) throws InterruptedException {
if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); }
try {
String name = "DCregistry";
dc = new Test1();
Naming.rebind(name, dc);
print("dc bound");
}
catch (java.rmi.RemoteException ex) {
print("Remote Exception");
ex.printStackTrace();;
}
catch (Exception e) {
print("Naming.rebind exception:");
e.printStackTrace();
}
try {
final InetAddress localaddr = InetAddress.getLocalHost();
print("Local IP Address : " + localaddr);
} catch (UnknownHostException e) {
print("Can't detect localhost : " + e);
}
init();
} // main
private static void init() {
try {
// (new Thread((Runnable) dc)).start();
Thread.sleep(1200);
} catch (Exception e) {
print(String.format("Could not connect to account W"));
e.printStackTrace();
}
} // init()
public static void print(Object o) {
System.out.println("DC server: " + o);
}
}
When I run it [from within Eclipse], I get
DC server: Naming.rebind exception:java.lang.IllegalArgumentException
at sun.net.www.ParseUtil.decode(Unknown Source)
at sun.net.www.protocol.file.Handler.openConnection(Unknown Source)
at sun.net.www.protocol.file.Handler.openConnection(Unknown Source)
at java.net.URL.openConnection(Unknown Source)
at sun.rmi.server.LoaderHandler.addPermissionsForURLs(Unknown Source)
at sun.rmi.server.LoaderHandler.getLoaderAccessControlContext(Unknown Source)
at sun.rmi.server.LoaderHandler.lookupLoader(Unknown Source)
at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadProxyClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(Unknown Source)
at java.io.ObjectInputStream.readProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at matram.RemoteDCmain.main(RemoteDCmain.java:16)
I'm really at a loss re where this is coming from. And I haven't even invoked the client yet...
Upvotes: 0
Views: 1171
Reputation: 311039
at sun.net.www.protocol.file.Handler.openConnection(Unknown Source)
You are using a file:
URL somewhere.
at sun.rmi.server.LoaderHandler.lookupLoader(Unknown Source)
You are using a class loader.
Conclusion: you are specifying an invalid file:
URL in the java.rmi.serve.codebase
system property.
I would also ask you why you're using a file:
URL at all. It won't be any use to the client unless the client is on the same host, in which case you don't actually need the codebase feature at all.
Upvotes: 1
Reputation: 96
One thing to remember for a better practice of integrated components in Java is the use of CMD. I'm not discouraging you for the use of eclipse but RMI is a different case. Here in Java RMI you need to start RMI registry on some free port (say 4545) explicitly by using the Command rmiregistry 4545
, Also you need to compile the Server Class not with javac
but with rmic
. One last thing; use a fully qualified url for rmi server address. Ex : rmic://localhost:4545/myServer
Upvotes: -1