RMI ConnectException:连接拒绝主机:127.0.0.1

时间:2016-11-20 11:08:35

标签: java xml ant rmi rmiregistry

我在运行我的Ant build.xml时收到此错误java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: [java] java.net.ConnectException: Connection refused (Connection refused),尽管我已经完成了迄今为止在论坛上发现的所有提示。

build.xml文件

<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;
    };

创建并运行RMI注册表

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);
    }
}

我的/ etc / hosts文件

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这只是值得尝试的事情 - 尝试打印InetAddress.getLocalHost().getHostAddress()。这可能听起来很奇怪,但在我的机器上它返回127.0.1.1而不是127.0.0.1

我相信这可以让事情变得更清楚:https://askubuntu.com/questions/754213/what-is-difference-between-localhost-address-127-0-0-1-and-127-0-1-1