RMI的问题.
做了一個(gè)簡(jiǎn)單的RMI例子,但是一直有問(wèn)題, 接口:
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
實(shí)現(xiàn)類
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 自動(dòng)生成方法存根
return "Server says,'hey'";
}
public static void main(String[] args) {
try {
// 產(chǎn)生遠(yuǎn)程對(duì)象
MyRemote service = new MyRemoteImp();
// 注冊(cè)服務(wù)
Naming.rebind("RemoteHello", service);
System.out.println("++++++++++++++");
} catch (Exception e) {
// TODO 自動(dòng)生成 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 自動(dòng)生成 catch 塊
e.printStackTrace();
} catch (RemoteException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
} catch (NotBoundException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
}
}
}
在DOS窗口編譯成功后執(zhí)行:
rmic rmi.MyRemoteImp(生成stub文件)
start rmiregistry (打開注冊(cè)服務(wù))
java rmi.MyRemoteImp(注冊(cè))
都順利執(zhí)行
打開另一DOS窗口:
執(zhí)行:java rmi.MyRemoteClient 出現(xiàn)下面錯(cuò)誤
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)
根據(jù)提示找到這行:
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");
出現(xiàn)了ClassCastException異常.
Naming.lookup返回的是Remote類.
why????
在網(wǎng)上看了很多貼子,也有人發(fā)生一樣樣的問(wèn)題,但是沒有找到解決辦法,大家有沒有思路啊?
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
實(shí)現(xiàn)類
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 自動(dòng)生成方法存根
return "Server says,'hey'";
}
public static void main(String[] args) {
try {
// 產(chǎn)生遠(yuǎn)程對(duì)象
MyRemote service = new MyRemoteImp();
// 注冊(cè)服務(wù)
Naming.rebind("RemoteHello", service);
System.out.println("++++++++++++++");
} catch (Exception e) {
// TODO 自動(dòng)生成 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 自動(dòng)生成 catch 塊
e.printStackTrace();
} catch (RemoteException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
} catch (NotBoundException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
}
}
}
在DOS窗口編譯成功后執(zhí)行:
rmic rmi.MyRemoteImp(生成stub文件)
start rmiregistry (打開注冊(cè)服務(wù))
java rmi.MyRemoteImp(注冊(cè))
都順利執(zhí)行
打開另一DOS窗口:
執(zhí)行:java rmi.MyRemoteClient 出現(xiàn)下面錯(cuò)誤
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)
根據(jù)提示找到這行:
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");
出現(xiàn)了ClassCastException異常.
Naming.lookup返回的是Remote類.
why????
在網(wǎng)上看了很多貼子,也有人發(fā)生一樣樣的問(wèn)題,但是沒有找到解決辦法,大家有沒有思路啊?
總結(jié)
- 上一篇: java的volatile是什么意思
- 下一篇: Tomcat5.5 配置mysql数据库