无法启动 RMI 斐波那契服务器



我正在学习Java RMI,我创建了一个非常简单的服务器来计算斐波那契数。服务器(FibonacciServer)创建一个负责计算序列的对象(Fibonacci),该对象实现一个接口(IFibonacci):

斐波那契服务器.java:

package myrmifibonacciserver;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class FibonacciServer {
    public static void main(String args[]){
        try{
            Fibonacci fib = new Fibonacci();
            Naming.rebind("fibonacci", fib);
            System.out.println("Fibonacci Server ready.");
        }catch(RemoteException rex){
            System.err.println("Exception in Fibonacci.main " + rex);
        } catch (MalformedURLException ex) {
            System.err.println("MalformedURLException " + ex);
        }
    }
}

斐波那契:

package myrmifibonacciserver;
import java.math.BigInteger;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Fibonacci extends UnicastRemoteObject implements IFibonacci{
    private static final long serialVersionUID = -4300545841720809981L;
    public Fibonacci() throws RemoteException{
        super();
    }
    @Override
    public BigInteger getFibonacci(int n) throws RemoteException {
        return getFibonacci(new BigInteger(Long.toString(n)));
    }
    @Override
    public BigInteger getFibonacci(BigInteger n) throws RemoteException {
        System.out.println("Calculating teh " + n + "th Fibonacci number");
        BigInteger zero = BigInteger.ZERO;
        BigInteger one = BigInteger.ONE;
        if(n.equals(zero) || n.equals(one)) 
            return one;
        BigInteger current = one;
        BigInteger low = one;
        BigInteger high = one;
        BigInteger temp;
        while(current.compareTo(n) == -1){
            temp = high;
            high = high.add(low);
            low = temp;
            current = current.add(one);
        }
        return high;
    }
}

IFibonacci:

package myrmifibonacciserver;
import java.math.BigInteger;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IFibonacci extends Remote{
    public BigInteger getFibonacci(int n) throws RemoteException;
    public BigInteger getFibonacci(BigInteger n) throws RemoteException;
}

如您所见,这是一个非常基本的示例。我正在使用命令 rmiregistry & 在 linux 上启动 RMI 注册表,它启动没有问题。

但是,当我单击运行按钮(在 Eclipse 或 Netbeans 中)来运行我的小项目时,出现错误:

Exception in Fibonacci.main java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: myrmifibonacciserver.IFibonacci

我也不知道为什么!起初我以为是因为存根,但由于我使用的是 java 1.7,所以它们是自动创建的。我做错了什么?

它没有找到代码库。原因是,从JDK 7开始,java.rmi.server.useCodebaseOnly属性默认为,而在以前的版本中,缺省为

当属性为 false 时,它使用 sever 的代码库,但在 true 的情况下,它会忽略它。

http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/enhancements-7.html

您的问题将在较低的 JDK 中解决JDK6

注册表在其 CLASSPATH 上没有可用的命名类和/或您没有正确设置代码库(如果有的话)。您不必使用代码库功能,但如果这样做,则必须是正确的。一个更简单的方法可能是通过 LocateRegistry.createRegistry() 在服务器 JVM 中启动注册表。

最新更新