尝试通过 Netbeans 测试 rmi 教程以设置远程服务器 - 客户端接口。运行服务器时出现以下异常:
异常:java.rmi.ServerException:RemoteException发生在服务器线程中;嵌套异常是:java.rmi.UnmarshalException:错误解组参数;嵌套异常是:java.lang.ClassNotFoundException:remotetests.MyRemoteInterface
rmi 注册表在后台运行。
似乎服务器无法发布远程对象??下面是服务器、远程接口和客户端类
请帮忙 - 谢谢!
服务器类如下所示:
package remotetests;
import java.io.Serializable;
import java.net.InetAddress;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.*;
public class MyRemoteClass extends UnicastRemoteObject implements MyRemoteInterface, serializable {
public MyRemoteClass() throws RemoteException {
//Constructor code
System.out.println("MyRemote constructor");
}
public void myMethod1() throws RemoteException {
//Here we put the code we want
System.out.println("I am in myMethod1()");
}
public int myMethod2() throws RemoteException {
return 5; //Here we put the code we want
}
public void anotherMethod() {
//If we define another method, this can´t be called in a remote way unless we call it from the remote interface
}
public static void main(String[] args) {
try {
System.out.println("running main");
MyRemoteInterface mir = new MyRemoteClass();
InetAddress address = java.net.InetAddress.getLocalHost();
System.out.println("host name : " + address.getHostName());
//Naming.rebind("//"+java.net.InetAddress.getLocalHost().getHostAddress() "/RMI_Test", mir);
LocateRegistry.getRegistry().rebind("//"+java.net.InetAddress.getLocalHost().getHostAddress(), mir);
//System.out.println("Server started. Bound RMI test");
} catch (Exception e) {
System.err.println("exception: " + e);
}
}
}
远程接口类:
package remotetests;
import java.rmi.*;
public interface MyRemoteInterface extends Remote {
public void myMethod1() throws RemoteException;
public int myMethod2() throws RemoteException;
}
客户端类:
package remotetests;
public class MyRMIClient {
public void MyRMIClient(String[] args){
try{
MyRemoteInterface mir = (MyRemoteInterface) java.rmi.Naming.lookup("//"+args[0] +":" +args[1]+"/RMITest");
//MyRemoteInterface mir = (MyRemoteInterface) java.rmi.Naming.lookup("//127.0.0.1:1099/RMITest/");
//We print myMethod1() as much as we get back myMethod2()
int monitor = mir.myMethod2();
for(int i=1;i<mir.myMethod2();i++){
mir.myMethod1();
}
}catch (Exception e){
e.printStackTrace();
}
}
}*