这是一个简单的RMI程序,当我运行HelloClient.java时,它总是抛出异常。
创建远程接口
public interface Hello extends Remote {
String sayHello(String name) throws RemoteException;
}
创建远程类
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello {
protected HelloImpl() throws RemoteException {
super();
}
@Override
public String sayHello(String name) throws RemoteException {
System.out.println("HelloImpl:" + name);
return name;
}
}
创建服务器:
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class HelloServer {
public static final int port = 1099;
public static void main(String[] args) {
try {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
Registry registry = LocateRegistry.createRegistry(port);
HelloImpl impl = new HelloImpl();
registry.rebind("//SEJ1T1DYN68BZBF:1099/HelloService", impl);
String[] names = registry.list();
for (String name : names) {
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
创建客户端:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class HelloClient {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry();
Hello hello = (Hello) registry
.lookup("//SEJ1T1DYN68BZBF:1099/HelloService");
System.out.println(hello.sayHello("javamaj blog"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
例外是:
java.rmi.ConnectIOException: non-JRMP server at remote endpoint
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at HelloClient.main(HelloClient.java:10)
环境: jdk 1.7 + eclipse + window xp
答案 0 :(得分:1)
在客户端主机的端口1099上运行RMI注册表以外的其他内容。
但是,除非客户端主机和服务器主机是同一主机,否则无论如何都要查找错误的注册表。您需要使用服务器主机名调用getRegistry()
,这样您就可以在服务器主机上查找注册表:服务器绑定的注册表。