无法启动RMI Fibonacci服务器 [英] Cannot launch RMI Fibonacci server

查看:114
本文介绍了无法启动RMI Fibonacci服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

FibonacciServer.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属性默认情况下为 true ,而在以前的版本中,默认情况下为 false .

当属性为 false 时,它将使用服务器的代码库,但在 true 的情况下,它将忽略它.

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

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

I am learning Java RMI and I have created a very simple server that calculates Fibonacci numbers. The server (FibonacciServer) creates an object responsible for calculating the sequence (Fibonacci) and that object implements an interface (IFibonacci):

FibonacciServer.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);
        }
    }
}

Fibonacci:

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;
}

As you can see, this is quite a basic example. I am launching the RMI registry on linux by using the command rmiregistry & and it starts without problems.

However, when I click the run button (in Eclipse or Netbeans) to run my little project, I get an error:

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

And I have no idea why! At first I thought it was because of the stubs, but since I am using java 1.7, those are created automatically. What am I doing wrong ?

解决方案

It's not finding the codebase. The reason is that, as of JDK 7, the java.rmi.server.useCodebaseOnly property is true by default, whereas in prior releases it was false by default.

When property is false then it uses the code base of sever but in true case it ignores it.

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

Your problem would resolve in lower JDK. ex JDK6

这篇关于无法启动RMI Fibonacci服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆