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

<p>build.xml file</p>



<pre><code>&lt;dirname property="basedir" file="." /&gt;

&lt;target name="compile"&gt;
    &lt;mkdir dir="${basedir}/bin" /&gt;
    &lt;javac srcdir="${basedir}/src" destdir="${basedir}/bin" /&gt;
&lt;/target&gt;

&lt;target name="clean" description="cleanup module"&gt;
    &lt;delete dir="${basedir}/bin" /&gt;
&lt;/target&gt;

&lt;target name="run" depends="compile"&gt;
    &lt;parallel&gt;

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

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

<p>client.policy</p>

<pre><code>grant{
       permission java.security.AllPermission;
     };
</code></pre>

<p>server.policy</p>

<pre><code>grant{
       permission java.security.AllPermission;
    };
</code></pre>

<p>Creating and running the RMI registry</p>

<pre><code>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);
    }
}
</code></pre>

<p>my /etc/hosts file</p>

<pre><code>127.0.0.1   localhost
127.0.0.1   mylaptop
127.0.0.1   postgres
127.0.0.1   java.rmi.server.hostname
</code></pre>

<p>Any help will be appreciated.</p>