Why does the localhost RMI client use nohosthost ip to connect the local RMI server

We use the connection between the client and the RMI server on the same computer (so all ip must be localhost).

We run the registry (using port 1099 by default)

registry = LocateRegistry.createRegistry(port);

and export some objects to the RMI registry

Naming.rebind("//" + "localhost" + ":" + port + "/" + name, object);

We extract some objects from another process (remember that everything runs on the local host)

MyRemoteObject ro = (MyRemoteObject) Naming.lookup("//" + "localhost" + ":" + port + "/" + name);

The problem occurs when you start an application running on the local network, and in the middle of the process you disconnect the network connection. If you run the application and an error pop-up does not work on the local network, and if you run the application, the error pop-up does not work on the local network. This only happens when the local network changes during application startup.

The exception that occurred while executing the Naming.lookup () method is the following:

java.lang.RuntimeException: java.rmi.ConnectIOException: Exception creating connection to: 192.168.xx; nested exception is: java.net.NoRouteToHostException: No route to host: connect

Debugging a little bit i learned that

RemoteObject ($ Proxy0) โ†’ RemoteObjectInvocationHandler โ†’ UnicastRef2 โ†’ LiveRef โ†’ TCPEndpoint

had a host ip (ex: 192.168.xx) instead of "localhost" or 127.0.0.1 (what I would like). And the isLocal boolean of the liveRef object is always false.

I do not know if this is enough. Sorry!!!

Do you have any suggestions?

My attempts:

I tried these solutions

  • Run jvm with the argument -Djava.rmi.server.hostname = localhost
  • Redefine RMIServerSocketFactory returns 127.0.0.1 each time. (TCPEndpoint has 192.168.xx ip and isLocal is always false)
  • Call the hostless retraining and search function in the URI. This is supposed to mean localhost.

but none of them worked.

Any suggestion would be welcome.

+6
java connection localhost rmi
source share
3 answers

java.rmi.server.hostname should do the trick . You installed it on the server, right?

If java.rmi.server.hostname=localhost does not work, what about java.rmi.server.hostname=127.0.0.1 or java.rmi.server.hostname=::1 ?

+5
source share

You can try to start your server using this set of properties -Djava.rmi.server.hostname=localhost

See also Section 3.5.4

This is due to the fact that the server communicates with the interface, either 127.0.0.1, or 192.168.xx

0
source share

Sorry guys. My mistake.

As you point out, I set the argument "java.rmi.server.hostname = localhost" for client processes. It started up like a charm when I set the argument to handle a running RMI server.

Many thanks.

0
source share

All Articles