2017-05-27 59 views
0

當我試圖發送一個對象作爲參數傳遞給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接口的基本類型,遠程對象和非遠程對象。有關如何使類可序列化的更多詳細信息,請參閱「Java對象序列化規範」。 RMI系統動態下載本地不可用的參數或返回值類。參見「動態類加載」一節中關於如何RMI下載參數和讀取參數時返回值類,返回值和異常的更多信息。 Here.

根據這一點,我在傳遞參數時一定沒有問題,但它們仍然是。

+0

你有GOOGLE上搜索這個execption?有關於它的很多主題https://stackoverflow.com/questions/6322107/java-no-security-manager-rmi-class-loader-disabled – Rjiuk

+0

@Rjiuk,是的,我做到了。但我無法弄清楚。 – Yaroslav

回答

1

您應該創建一個新的類,繼承UnicastRemoteObject,同時實現您的功能,這將是能夠通過RMI參數發送這樣的對象沒有任何問題。

於是,我改變你的函數FunctionInterface,創建新類功能:

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

和它的作品!

相關問題