java.rmi.ConnectException: Connection refused to host: 127.0.1.1;

232,683

Solution 1

This is item A.1 in the RMI FAQ. You need to either fix your /etc/hosts file or set the java.rmi.server.hostname property at the server.

Solution 2

PROBLEM SOLVED

I had exactly the same error. When the remote object got binded to the rmiregistry it was attached with the loopback IP Address which will obviously fail if you try to invoke a method from a remote address. In order to fix this we need to set the java.rmi.server.hostname property to the IP address where other devices can reach your rmiregistry over the network. It doesn't work when you try to set the parameter through the JVM. It worked for me just by adding the following line to my code just before binding the object to the rmiregistry:

System.setProperty("java.rmi.server.hostname","192.168.1.2");

In this case the IP address on the local network of the PC binding the remote object on the RMI Registry is 192.168.1.2.

Solution 3

you can use LocateRegistry such as:

Registry rgsty = LocateRegistry.createRegistry(1888);
rgsty.rebind("hello", hello);

Solution 4

I found many of the Q&A on this topic, not nothing was helping me - that's because my issue was more basic ( what can I say I am not a networking guru :) ). My ip address in /etc/hosts was incorrect. What I had tried included the following for CATALINA_OPTS:

CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true -Xmx128M -server 
-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=7091 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=A.B.C.D"  #howeverI put the wrong ip here!

export CATALINA_OPTS

My problem was that I had changed my ip address many months ago, but never updated my /etc/hosts file. it seems that by default the jconsole uses the hostname -i ip address in some fashion even though I was viewing local processes. The best solution was to simply change the /etc/hosts file.

The other solution which can work is to get your correct ip address from /sbin/ifconfig and use that ip address when specifying the ip address in, for example, a catalina.sh script:

-Djava.rmi.server.hostname=A.B.C.D

Solution 5

Maybe your rmiregistry not be created before client trying connect to your server and it would lead to this exception.In Linux, you can use "netstat" to check your rmiregistry be bond on the right port you assigned in java code.

Share:
232,683
heythatsmekri
Author by

heythatsmekri

Updated on January 11, 2022

Comments

  • heythatsmekri
    heythatsmekri over 2 years
        java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is:
        java.net.ConnectException: Connection refused
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:128)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
        at com.sun.proxy.$Proxy0.notifyMe(Unknown Source)
        at CallbackServerImpl.doCallback(CallbackServerImpl.java:149)
        at CallbackServerImpl.registerForCallback(CallbackServerImpl.java:70)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
        at sun.rmi.transport.Transport$1.run(Transport.java:177)
        at sun.rmi.transport.Transport$1.run(Transport.java:174)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:722)
        Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
        at java.net.Socket.connect(Socket.java:579)
        at java.net.Socket.connect(Socket.java:528)
        at java.net.Socket.<init>(Socket.java:425)
        at java.net.Socket.<init>(Socket.java:208)
        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
        ... 23 more
    

    I get this exception when I try to connect a remote client to my server. In both, server and client the hostName for the registryUrl of rmi is the public IP address of the server. I also tried to put localhost in server but the error doesn't change.

    My java.policy is set to grant all connections to all ports and I have no firewalls enabled in the server or the client.

    Any suggestions what could be?

  • user207421
    user207421 almost 10 years
    It wouldn't cause this exception with this stacktrace.
  • user207421
    user207421 almost 10 years
    The Registry does not get bound to 127.0.0.1 If the system property didn't work from the command line you did it wrong.
  • researcher
    researcher over 9 years
    Please, how are you rsolved this problem ? i try with: java HelloServer -Djava.rmi.server.hostname=192.168.1.5 but the client cant' find the server java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: java.net.ConnectException: Connexion refusée
  • user207421
    user207421 almost 9 years
    @researcher You have to set it at the server JVM. Not the client.
  • user207421
    user207421 almost 9 years
    It does work when you 'try to set the parameter through the JVM', by which you presumably mean on the command line. If you use System.setProperty(), you have to set it before exporting the remote object, not just before binding.
  • alisianoi
    alisianoi over 7 years
    Oh but it would, observing it on my Archlinux box now. Creating the registry with LocateRegistry.createRegistry(1099) in my server class rather than getting it with LocateRegistry.getRegistry() solves the problem for me.
  • user207421
    user207421 over 6 years
    It solves another problem. It solves 'connection refused' when doing the lookup. This problem is 'connection refused' when calling the remote method.
  • user207421
    user207421 over 6 years
    There is no 'must' about it. It's only necessary with misconfigured DNS, or in some multi-homed host situations.
  • user207421
    user207421 over 6 years
    @researcher And you also have to provide JVM arguments before the class name, not afterwards.
  • qrtLs
    qrtLs about 6 years
    the 'server' is the skeleton side on which the stub was originally created via .export; on the server side modify the implicitly stub embedded ip the client invokes: System.setProperty("java.rmi.server.hostname","1.2.3.4"); meaning this is the ip the destination/skeleton/server is approachable
  • Patrick Bucher
    Patrick Bucher about 6 years
    @researcher After endless hours of trying this and that, defining the server hostname finally did it! I am so grateful! THANK. YOU.
  • user207421
    user207421 over 4 years
    If the port was already occupied you didn't get 'connection refused', and changing the Regsitry to another port didn't fix anything unless you also adjusted the code, and port 1099 was reserved for the RMI Registry by 1996, so whatever alleged process was allegedly already listening to it was severally non-compliant.
  • Davide
    Davide over 4 years
    Thanks, I just modify /etc/hosts to point my localhost and hostname to the same ip, as you show, and now all works
  • mjuarez
    mjuarez over 4 years
    Welcome to StackOverflow. This doesn't answer the original question.
  • user207421
    user207421 about 4 years
    @PatrickBucher The person you mention only contributed a question. You should be thanking is me.
  • user207421
    user207421 over 3 years
    He is not getting an eror on rebind().
  • user207421
    user207421 over 3 years
    This is not correct. The URL you use for the Registry does not determine what IP address the remote object is bound to. The IP bind has already happened prior to this code.