ArrayList的RMI应用程序

问题描述

我想问的是,在编写此代码进行作业时。已经解决了。我外出2小时后,代码运行不正常,并弹出很多错误消息。

在这个论坛中,我将提供客户端,服务器,接口,类和RMIregistry Java文件代码

客户端

    private void menuancestorAdded(javax.swing.event.AncestorEvent evt) {                                   
      try{ 
        Registry reg = LocateRegistry.getRegistry("127.0.0.1",1045);
        Interface f = (Interface)reg.lookup("cmenu");
        System.out.println("run");
        ArrayList<foodorder> list = new ArrayList<foodorder>(); 
        System.out.println("run1");
        list =  f.cmenu(); // This error when I run the code
        System.out.println("run2");
        for(foodorder r:list){
            System.out.println(r.getMenu_name());
        }
 
      }catch (Exception ex){
          ex.printstacktrace();
      }
    }   

服务器

// To view the menu for the customers
        public ArrayList <foodorder> cmenu()throws remoteexception
        {
            try{
          sql = "SELECT ItemName,Price FROM Foodorder"
               + " WHERE Quantity >= 1";
          ResultSet a = stmt.executeQuery(sql);
          System.out.println("Execute= " + sql);
          while(a.next()){
              cart.add(new foodorder(a.getString(1),a.getDouble(2)));
          }    
          } catch (sqlException ex){
              ex.printstacktrace();
          }
         return cart;
        }

课程

public class foodorder {
    private double price;
    private String menu_name;

    public foodorder(String menu_name,double price) {
        this.price = price;
        this.menu_name = menu_name;
    }
}

界面

public interface Interface extends Remote{
    public ArrayList <foodorder> cmenu()throws remoteexception;
  
}

RMIRegister:

public class Rmiregistry {
public static void main(String[] args) throws remoteexception{
        
    Registry reg = LocateRegistry.createRegistry(1045);
    reg.rebind("cmenu",new Server()); // To view the menu for the customer
    }    
}

错误

run
run1
java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: food.order.system.foodorder
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:193)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)