【问题标题】:RMI error while trying to get an applet from the server尝试从服务器获取小程序时出现 RMI 错误
【发布时间】:2015-06-26 11:13:10
【问题描述】:

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

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

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

这是客户端代码

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

这是小程序代码:

public class simpleApplet extends Applet {

    JLabel l= new JLabel("120");

    public void init() {

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

【问题讨论】:

    标签: java applet rmi


    【解决方案1】:

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

    【讨论】:

    • 但是如果我这样做我的整个程序将毫无用处,客户端将能够获取小程序而无需访问服务器
    • 正确。所以也许你的整个程序没用的。除非您使用 RMI 代码库功能,否则您不能这样做。
    • 或者也许您应该编写一个网页而不是客户端,这才是小程序真正(或曾经)的用途。
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2011-06-16
    • 2021-04-29
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    相关资源
    最近更新 更多