编写Java RMI聊天应用程序时获取NotBoundException:-/

编写Java RMI聊天应用程序时获取NotBoundException:-/,java,rmi,Java,Rmi,我已经编写了一个JavaRMI聊天应用程序。有四个类和两个接口。这是: 聊天客户端 } 聊天客户端驱动程序 聊天客户端接口 聊天服务器 聊天服务器接口 当我在Commando上运行它时,首先运行RMICChatClient和ChatServer,然后运行RMICRegistry。然后我运行chatServerDriver,它工作得非常好。之后,当我运行名为chatClientDriver的chatClientDriver时,出现以下错误,我不明白为什么:/我可以得到任何解决方案吗 谢谢:) 看起

我已经编写了一个JavaRMI聊天应用程序。有四个类和两个接口。这是:

聊天客户端

}

聊天客户端驱动程序

聊天客户端接口

聊天服务器

聊天服务器接口

当我在Commando上运行它时,首先运行RMICChatClient和ChatServer,然后运行RMICRegistry。然后我运行chatServerDriver,它工作得非常好。之后,当我运行名为chatClientDriver的chatClientDriver时,出现以下错误,我不明白为什么:/我可以得到任何解决方案吗

谢谢:)


看起来您在重新绑定中的地址与客户端用于连接的地址不同

Naming.rebind("//localhost/RMIChatServer", new ChatServer());
下面的Wikipedia页面上有一个示例实现,可能值得与您的代码进行比较。


请注意,使用Java 1.5+时,您不再需要使用rmic。请参见

,看起来您的重新绑定地址与客户端用于连接的地址不同

Naming.rebind("//localhost/RMIChatServer", new ChatServer());
下面的Wikipedia页面上有一个示例实现,可能值得与您的代码进行比较。

请注意,使用Java1.5+您不再需要使用rmic,请参见

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ChatClientIF extends Remote {
void retrieveMessage(String message) throws RemoteException;
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import com.za.tutorial.rmi.client.ChatClientIF;

public class ChatServer extends UnicastRemoteObject implements ChatServerIF {


private ArrayList<ChatClientIF> chatClients;
protected ChatServer() throws RemoteException {
    chatClients = new ArrayList<ChatClientIF>();
    }

public synchronized void registerChatClient(ChatClientIF chatClient)
        throws RemoteException {
    this.chatClients.add(chatClient);
}

public synchronized void broadcastMessage(String message) throws RemoteException {
    int i = 0;
    while(i < chatClients.size()){
        chatClients.get(i++).retrieveMessage(message);
    }
}
}
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;

public class ChatServerDriver {

public static void main(String[] args) throws RemoteException, MalformedURLException {
Naming.rebind("RMIChatServer", new ChatServer());
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
import com.za.tutorial.rmi.client.ChatClientIF;

public interface ChatServerIF extends Remote {
void registerChatClient(ChatClientIF chatClient) throws RemoteException;
void broadcastMessage(String message) throws RemoteException;
}
Exception in thread "main" java.rmi.NotBoundException: RMIChatServer
    at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:136)
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Unknown Source)
    at com.za.tutorial.rmi.client.ChatClientDriver.main(ChatClientDriver.java:15)
Naming.rebind("//localhost/RMIChatServer", new ChatServer());