做了一个简单的RMI例子,但是一直有问题, 接口:
实现类
客户端类:
在DOS窗口编译成功后执行:
rmic rmi.MyRemoteImp(生成stub文件)
start rmiregistry (打开注册服务)
java rmi.MyRemoteImp(注册)
都顺利执行
打开另一DOS窗口:
执行:java rmi.MyRemoteClient 出现下面错误
Exception in thread "main" java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub
at rmi.MyRemoteClient.go(MyRemoteClient.java:19)
at rmi.MyRemoteClient.main(MyRemoteClient.java:8)
根据提示找到这行:
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");
出现了ClassCastException异常.
Naming.lookup返回的是Remote类.
why????
在网上看了很多贴子,也有人发生一样样的问题,但是没有找到解决办法,大家有没有思路啊?
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
实现类
package rmi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MyRemoteImp extends UnicastRemoteObject implements MyRemote {
protected MyRemoteImp() throws RemoteException {
}
public String sayHello() throws RemoteException {
// TODO 自动生成方法存根
return "Server says,'hey'";
}
public static void main(String[] args) {
try {
// 产生远程对象
MyRemote service = new MyRemoteImp();
// 注册服务
Naming.rebind("RemoteHello", service);
System.out.println("++++++++++++++");
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
客户端类:
package rmi;
import java.net.MalformedURLException;
import java.rmi.*;
public class MyRemoteClient {
public static void main(String args[]) {
new MyRemoteClient().go();
}
public void go() {
try {
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");
String s=service.sayHello();
System.out.println(s);
} catch (MalformedURLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (RemoteException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (NotBoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
在DOS窗口编译成功后执行:
rmic rmi.MyRemoteImp(生成stub文件)
start rmiregistry (打开注册服务)
java rmi.MyRemoteImp(注册)
都顺利执行
打开另一DOS窗口:
执行:java rmi.MyRemoteClient 出现下面错误
Exception in thread "main" java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub
at rmi.MyRemoteClient.go(MyRemoteClient.java:19)
at rmi.MyRemoteClient.main(MyRemoteClient.java:8)
根据提示找到这行:
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");
出现了ClassCastException异常.
Naming.lookup返回的是Remote类.
why????
在网上看了很多贴子,也有人发生一样样的问题,但是没有找到解决办法,大家有没有思路啊?