我在网上下载一个学习rmi的例子程序,调试很长时间也没有没有调通,追后的错误如下:
Trobulett:java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: CalculatorImpl_Stub
我在此对程序做个说明:
我将所有的文件保存在D:\rmiDemo这个目录下面总共有四个java文件:
1)接口 Calculator.java
import java.rmi.Remote;
public interface Calculator extends Remote{
public long add(long a,long b)throws java.rmi.RemoteException;
public long sub(long a,long b)throws java.rmi.RemoteException;
public long mul(long a,long b)throws java.rmi.RemoteException;
public long div(long a,long b)throws java.rmi.RemoteException;
}
2)接口的具体实现 CalculatorImpl.java
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl extends UnicastRemoteObject implements Calculator{
//这个实现必须有一个显示的构造函数,并且要抛出一个RemoteException异常
public CalculatorImpl() throws java.rmi.RemoteException {
super();
}
public long add(long a,long b) throws java.rmi.RemoteException {
return a+b;
}
public long sub(long a,long b) throws java.rmi.RemoteException {
return a-b;
}
public long mul(long a,long b) throws java.rmi.RemoteException {
return a*b;
}
public long div(long a,long b) throws java.rmi.RemoteException {
return a/b;
}
}
3)客户端 CalculatorClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient{
public static void main(String[] args){
try{
Calculator c = (Calculator)Naming.lookup("rmi://localhost/CalculatorService");
System.out.println(c.sub(4,3));
System.out.println(c.add(4,5));
System.out.println(c.mul(3,6));
System.out.println(c.div(9,3));
}catch (MalformedURLException murle) {
System.out.println();
System.out.println(
"MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println(
"RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println(
"NotBoundException");
System.out.println(nbe);
}
catch (
java.lang.ArithmeticException
ae) {
System.out.println();
System.out.println(
"java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
4)主机服务器 CalculatorServer.java
import java.rmi.Naming;
//一个服务器类
public class CalculatorServer{
public CalculatorServer(){
try{
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService",c);