【问题标题】:RMI ConnectException: Connection refused to host: 127.0.0.1RMI ConnectException:连接拒绝主机:127.0.0.1
【发布时间】:2016-11-20 11:08:35
【问题描述】:

我在运行我的 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

任何帮助将不胜感激。

【问题讨论】:

    标签: java xml ant rmi rmiregistry


    【解决方案1】:

    这只是值得尝试的东西 - 尝试打印 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

    【讨论】:

    • 感谢您的回复。我已经试过了。它打印 127.0.0.1
    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2014-05-31
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多