views:

2697

answers:

4

I have a problem using java rmi:

When I'm trying to run my server, I get a connectException (see below).

Exception happens when executing the rebind method:

Runtime.getRuntime().exec("rmiregistry 2020");
MyServer server = new MyServer();
Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);

when using rmi://localhost:2020/RemoteDataPointHandler instead, it doesn't work either. Also using the default port does not work. I also tried using the 127.0.0.1 ip-address, but with the same effect.

my runtime args:

-Djava.security.policy=java.security.AllPermission

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306) at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) at java.rmi.Naming.rebind(Naming.java:160) at be.fortega.knx.server.Main.(Main.java:25) at be.fortega.knx.server.Main.main(Main.java:16) Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433) at java.net.Socket.connect(Socket.java:524) at java.net.Socket.connect(Socket.java:474) at java.net.Socket.(Socket.java:371) at java.net.Socket.(Socket.java:184) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569) ... 7 more

A: 

My first theory is that you have no nameserver / registry running where Naming expects it to be running. Try changing the "local" to some random local machine and see if the exception message changes. If it does not, that is evidence that this is a problem talking to the registry rather than a problem with the endpoint you are trying to register. (The ConnectException you are seeing is a subtype of RemoteException, that the javadoc says is thrown "if registry could not be contacted".)

But it may also be an endpoint issue. Logically, a "localhost" endpoint won't be accessible of your current machine. If something off-machine (including the registry) tries to use a "localhost" endpoint, they will end up talking to the wrong place. Even if this is not the direct cause of your problem, you may want to reconsider using "localhost".

Stephen C
A: 

It seems to work when I replace the

Runtime.getRuntime().exec("rmiregistry 2020");

by

LocateRegistry.createRegistry(2020);

anyone an idea why? What's the difference?

Fortega
+1  A: 

You need to have a rmiregistry running before attempting to connect (register) a RMI service with it.

The LocateRegistry.createRegistry(2020) method call creates and exports a registry on the specified port number.

See the documentation for LocateRegistry

Steen
+1  A: 

had a simliar problem with that connection exception. it is thrown either when the registry is not started yet (like in your case) or when the registry is already unexported (like in my case).

but a short comment to the difference between the 2 ways to start the registry:

Runtime.getRuntime().exec("rmiregistry 2020");

runs the rmiregistry.exe in javas bin-directory in a new process and continues parallel with your java code.

LocateRegistry.createRegistry(2020);

the rmi method call starts the registry, returns the reference to that registry remote object and then continues with the next statement.

in your case the registry is not started in time when you try to bind your object

axel
Thanks for the explanation!
Fortega