RMI的问题.
做了一個簡單的RMI例子,但是一直有問題, 接口:
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????
在網上看了很多貼子,也有人發生一樣樣的問題,但是沒有找到解決辦法,大家有沒有思路啊?
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????
在網上看了很多貼子,也有人發生一樣樣的問題,但是沒有找到解決辦法,大家有沒有思路啊?
總結
- 上一篇: java的volatile是什么意思
- 下一篇: struts-config.xml 简述