使用ant执行时的Java RMI NotBoundException

时间:2013-11-05 15:44:57

标签: java ant rmi

我正在尝试用Java RMI编写一个小游戏,我必须使用Apache Ant来运行它。 不幸的是,当我使用ant来执行服务器和客户端jar时,它会崩溃并出现NotBoundException。但是当我通过命令行(java -jar ...)执行jar时,一切正常。我无法弄清楚蚂蚁脚本出了什么问题。

重要的服务器代码:

public class GameServer extends UnicastRemoteObject implements IGameServer {

    public GameServer() throws RemoteException {
        //...
    }

    // some methods here

    public static void main(String[] args) throws RemoteException {
        RemoteServer.setLog(System.out);

        // create new GameServer Object and make it available via the Registry
        LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        Registry registry = LocateRegistry.getRegistry();
        GameServer server = new GameServer();
        registry.rebind("GameServer", server);
    }

}

重要客户代码:

public class GameClient extends UnicastRemoteObject implements IGameClient {

    public GameClient(String name) throws RemoteException, NotBoundException {
        this.name = name;
        Registry registry = LocateRegistry.getRegistry();
        server = (IGameServer) registry.lookup("GameServer");
        server.login(this.name, this);
    }

    // some methods here    

}

重要的蚂蚁代码:

<target name="jar" depends="compile">
    <jar destfile="${export}/FlyHuntClient.jar">
        <fileset dir="${build}" />
        <fileset dir="." includes="res/**" />
        <manifest>
            <attribute name="Main-Class" value="client.GameClientLauncher" />
            <attribute name="Class-Path" value="./" />
            <attribute name="Permissions" value="all-permissions" />
        </manifest>
    </jar>

    <jar destfile="${export}/FlyHuntServer.jar">
        <fileset dir="${build}" />
        <manifest>
            <attribute name="Main-Class" value="server.GameServer" />
            <attribute name="Class-Path" value="./" />
            <attribute name="Permissions" value="all-permissions" />
        </manifest>
    </jar>
</target>

<target name="run">
    <parallel>
        <java jar="${export}/FlyHuntServer.jar" fork="true" />
        <java jar="${export}/FlyHuntClient.jar" fork="true" />
    </parallel>
</target>

例外:

$ ant run
Buildfile: [trimmed...]/build.xml

run:
     [java] Nov 05, 2013 4:21:12 PM sun.rmi.server.UnicastServerRef logCall
     [java] Feiner: RMI TCP Connection(1)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
     [java] Nov 05, 2013 4:21:12 PM sun.rmi.server.UnicastServerRef logCall
     [java] Feiner: RMI TCP Connection(2)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: void rebind(java.lang.String, java.rmi.Remote)]
     [java] Nov 05, 2013 4:21:12 PM sun.rmi.server.UnicastServerRef logCallException
     [java] Fein: RMI TCP Connection(1)-127.0.0.1: [127.0.0.1] exception: 
     [java] java.rmi.NotBoundException: GameServer
     [java]     at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:137)
     [java]     at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
     [java]     at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409)
     [java]     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
     [java]     at sun.rmi.transport.Transport$1.run(Transport.java:177)
     [java]     at sun.rmi.transport.Transport$1.run(Transport.java:174)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
     [java]     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
     [java]     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
     [java]     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
     [java]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
     [java]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
     [java]     at java.lang.Thread.run(Thread.java:744)
     [java] 
     [java] Nov 05, 2013 4:21:12 PM sun.rmi.server.UnicastServerRef logCall
     [java] Feiner: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
     [java] Exception in thread "main" java.rmi.NotBoundException: GameServer
     [java]     at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:137)
     [java]     at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
     [java]     at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409)
     [java]     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
     [java]     at sun.rmi.transport.Transport$1.run(Transport.java:177)
     [java]     at sun.rmi.transport.Transport$1.run(Transport.java:174)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
     [java]     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
     [java]     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
     [java]     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
     [java]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
     [java]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
     [java]     at java.lang.Thread.run(Thread.java:744)
     [java]     at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)
     [java]     at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)
     [java]     at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378)
     [java]     at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
     [java]     at client.GameClient.<init>(Unknown Source)
     [java]     at client.GameClientLauncher.main(Unknown Source)

2 个答案:

答案 0 :(得分:0)

除非您希望所有游戏条款与服务器在同一主机上运行,​​否则您需要在服务器而不是客户端查找注册表。更改getRegistry()调用以指定服务器主机名。

答案 1 :(得分:-1)

您将GameServer绑定为存根中的远程对象,并尝试将其作为IgameServer。可能这可能是问题所在。

Registry registry = LocateRegistry.createRegistry(1099); 

Server server = new Server(); 

IGameServer iGameServer = (IGameServer) UnicastRemoteObject.exportObject(server, 0); 

// Bind the remote object's stub in the registry 

String bindingName = "Server"; 

registry.bind(bindingName, iGameServer); 

System.out.println("LOG DBG :[Server Ready Binding As " + bindingName +"]"); 

试试这个。这里不需要实现UnicastRemoteObject,因为接口已经实现了它。