通过rmi参数发送对象时出现异常

时间:2017-05-27 15:22:35

标签: java serialization deserialization rmi

当我尝试将对象作为参数发送给rmi时,我得到了这样的异常:

HelloClient exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: IntegralClient$1 (no security manager: RMI class loader disabled)

您能否帮我弄清楚我的代码和rmi服务器 - 客户端关系实现有什么问题?

这是我的代码:

服务器端:

import java.rmi.*;
import java.rmi.server.*; 

public class IntegralServer
{
    public static void main (String[] argv) 
    {
         try 
         {
             IntegralComputeEngine integralCounter = new IntegralComputeEngine();
             Naming.rebind("rmi://localhost/integral", integralCounter);
             System.out.println("Integral Server is ready.");
         }
         catch (Exception e)
         {
             System.out.println("Addition Server failed: " + e);
         }
    }
}

客户方:

import java.rmi.*;

public class IntegralClient
{
    public static void main(String[] args) 
    {
        try 
        {
            IntegralComputeEngineInterface integralCounter = (IntegralComputeEngineInterface) Naming.lookup("rmi://localhost/integral");
            double result = integralCounter.computeIntegral(createFunction(), -1, 1);
            System.out.println("Result is: " + result);
        } 
        catch (Exception e) 
        {
            System.out.println("HelloClient exception: " + e);
        }
    }

    private static Function createFunction() 
    {
        return new Function()
        {
            @Override
            public double valueIn(double x) 
            {
                return 1-x;
            }
        };
    }
}

功能接口(驻留在客户端和服务器项目中):

import java.io.Serializable;

public interface Function extends Serializable
{
    public double valueIn(double x);
}

IntegralComputeEngineInterface(驻留在客户端和服务器项目中):

import java.rmi.*;

public interface IntegralComputeEngineInterface extends Remote 
{
    public double computeIntegral(Function function, double from, double to) throws RemoteException;
}

IntegralComputeEngineInterface(仅驻留在服务器上):

import java.rmi.*;
import java.rmi.server.*;

public class IntegralComputeEngine  extends UnicastRemoteObject implements IntegralComputeEngineInterface 
{
    private final double ACCURACY = 100;

    protected IntegralComputeEngine() throws RemoteException 
    {super();}

    @Override
    public double computeIntegral(Function function, double from, double to) throws RemoteException 
    {
        double stepValue = (to - from) / ACCURACY;  
        double stepLength = Math.abs(stepValue);
        double result = 0.0; 

        for(int i = 0; i < ACCURACY; i++)
        {result += stepLength * function.valueIn(from + stepValue*(i + 0.5));}

        return result;
    }
}

以下是我的项目组织的截图:

enter image description here

对于我的问题,我将不胜感激!

我发现了这个: 远程对象的参数或返回值可以是任何可序列化的对象。这包括原始类型,远程对象和实现java.io.Serializable接口的非远程对象。有关如何使类可序列化的更多详细信息,请参阅&#34; Java对象序列化规范。&#34; RMI系统动态下载本地不可用的参数或返回值的类。请参阅&#34;动态类加载&#34;有关RMI在读取参数,返回值和异常时如何下载参数和返回值类的更多信息。  Here.

根据这一点,我在传递论证时一定没有问题,但它们仍然存在。

1 个答案:

答案 0 :(得分:1)

您应该创建一个新类,扩展UnicastRemoteObject并同时实现您的Function,它将能够通过rmi参数发送此类对象而不会出现任何问题。

因此,我将Function更改为FunctionInterface,创建了新的类Function:

import java.rmi.RemoteException;
import java.rmi.server.*;

public class Function extends UnicastRemoteObject implements FunctionInterface
{
    protected Function() throws RemoteException
    {super();}

    @Override
    public double valueIn(double x) 
    {
        return 1-x;
    }
}

所以我的IntegralClient现在看起来如此:

import java.rmi.*;

public class IntegralClient
{
    public static void main(String[] args) 
    {
        try 
        {
            IntegralComputeEngineInterface integralCounter = (IntegralComputeEngineInterface) Naming.lookup("rmi://localhost/integral");
            double result = integralCounter.computeIntegral(createFunction(), -1, 1);
            System.out.println("Result is: " + result);
        } 
        catch (Exception e) 
        {
            System.out.println("HelloClient exception: " + e);
        }
    }

    private static Function createFunction() throws RemoteException 
    {
//      return new FunctionInterface()
//      {
//          @Override
//          public double valueIn(double x) 
//          {
//              return 1-x;
//          }
//      };

        return new Function();
    }
}

它有效!