RMI ConnectException: Connection refused to host: 127.0.0.1

5.3k Views Asked by At

I am getting this error java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: [java] java.net.ConnectException: Connection refused (Connection refused) while running my Ant build.xml although I have done all the tips I found so far on the forum.

build.xml file

<dirname property="basedir" file="." />

<target name="compile">
    <mkdir dir="${basedir}/bin" />
    <javac srcdir="${basedir}/src" destdir="${basedir}/bin" />
</target>

<target name="clean" description="cleanup module">
    <delete dir="${basedir}/bin" />
</target>

<target name="run" depends="compile">
    <parallel>

        <java classpath="${basedir}/bin" classname="com.main.MainServer"
            fork="true">
            <jvmarg value="-Djava.security.policy=server.policy" />
        </java>

        <sequential>
            <java classpath="${basedir}/bin" classname="com.main.MainClient">
                <jvmarg value="-Djava.security.policy=client.policy" />
            </java>
        </sequential>
    </parallel>
</target>   

client.policy

grant{
       permission java.security.AllPermission;
     };

server.policy

grant{
       permission java.security.AllPermission;
    };

Creating and running the RMI registry

public void bindServer() {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
    try {   
        System.setProperty("java.rmi.server.hostname", InetAddress.getLocalHost().getHostAddress());
        server = (IServer) UnicastRemoteObject.exportObject(new Server(), IConstant.RMI_PORT_SERVER);
        Registry registry = LocateRegistry.createRegistry(IConstant.RMI_PORT_SERVER);                                   
        registry.rebind(IConstant.RMI_ID_SERVER, server);
    } catch (RemoteException e) {
        System.err.println("Server RemoteException:");
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}

my /etc/hosts file

127.0.0.1   localhost
127.0.0.1   mylaptop
127.0.0.1   postgres
127.0.0.1   java.rmi.server.hostname

Any help will be appreciated.

1

There are 1 best solutions below

1
Adrian Jałoszewski On

It's just something worth trying - try to print InetAddress.getLocalHost().getHostAddress(). It might sound strange but on my machine it returns 127.0.1.1 instead of 127.0.0.1.

I believe this could make some thing clearer: https://askubuntu.com/questions/754213/what-is-difference-between-localhost-address-127-0-0-1-and-127-0-1-1