我正在尝试用 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)