尝试从服务器获取applet时出现RMI错误

时间:2015-06-26 11:13:11

标签: java applet rmi

我正在研究一个简单的RMI示例,客户端基本上会尝试从服务器调用applet。所以我定义了一个名为“simpleApplet”的类(扩展Applet),在客户端我有两个文件:服务器接口的java文件和客户端代码的java文件。在服务器端,我有三个文件:一个用于我的applet,一个用于服务器接口,一个用于实现。 服务器运行正常,但我在客户端收到错误,这里是:

  

java.rmi.UnmarshalException:错误解组返回;嵌套   异常是:java.lang.ClassNotFoundException:simpleApplet(没有   安全管理器:禁用RMI类加载器)

我认为我的applet对象存在问题,客户端无法识别,我尝试使用强制转换(使用Applet),但我仍有问题。你会碰巧知道如何解决这个问题吗? 谢谢 !

这是客户端代码

public class SwingCall {

    static Applet a;

    public static void main(String[] args) throws Exception {

        myRmiServerIntf obj = (myRmiServerIntf) Naming.lookup("rmi://10.100.162.203:1100/newRmiServer");
        a= (Applet) obj.getApplet();

        JFrame frame = new JFrame("Window");
        frame.setVisible(true);
        frame.setSize(500, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);
        JButton button = new JButton("Call applet");
        panel.add(button);
        button.addActionListener(new Action1());

    }

    static class Action1 implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            a.init();

            JFrame frame = new JFrame("myApplet");
            frame.setVisible(true);
            frame.setSize(500, 500);
            frame.getContentPane().add(a);
            frame.setVisible(true);
        }
    }
}

这是服务器端代码:

public class myRmiServer extends UnicastRemoteObject implements myRmiServerIntf {

        public static final String MESSAGE = "Hello World";

        public myRmiServer() throws RemoteException {
            super(0);    // required to avoid the 'rmic' step, see below
        }

        public Applet getApplet() {
            simpleApplet app= new simpleApplet();
            return (Applet)app;
        }

        public static void main(String args[]) throws Exception {
            System.out.println("RMI server started");

            try { //special exception handler for registry creation
                LocateRegistry.createRegistry(1101); 
                System.out.println("java RMI registry created.");
            } catch (RemoteException e) {
                //do nothing, error means registry already exists
                System.out.println("java RMI registry already exists.");
            }

            //Instantiate RmiServer
            myRmiServer obj = new myRmiServer();
            System.out.println("After");
            // Bind this object instance to the name "newRmiServer"
            Naming.rebind("rmi://localhost:1101/newRmiServer", obj);
            System.out.println("PeerServer bound in registry");
        }
}

以下是applet代码:

public class simpleApplet extends Applet {

    JLabel l= new JLabel("120");

    public void init() {

        super.resize(500, 200);
        super.add(l);
        super.init();
    }
}

1 个答案:

答案 0 :(得分:0)

需要在客户端部署simpleApplet类文件。