如何通过HTTP服务器将Java RMI与动态类加载一起使用?

时间:2019-03-23 02:34:46

标签: java rmi

我正在尝试使用Java RMI框架实现基本的分布式应用程序。

我可以在本地实现它,但是当我要远程管理动态类加载时,可以通过在“远程”(使用同一局域网中的虚拟机)上将类的相应.class文件加载到HTTP服务器上来进行。 ),则客户端无法正确下载存根的.class:

Exception in thread "main" java.lang.NoClassDefFoundError: Example1 / RemoteAdd).

我试图在Windows计算机中与RMI注册表一起运行服务器,并在另一台具有Ubuntu(通过LAN连接)的虚拟机中运行客户端(在该计算机中也有带有文件的HTTP Web服务器)。类)。 但是什么都没有,我得到了例外:

Exception in thread "main" java.lang.NoClassDefFoundError: Example1 / RemoteAdd.

IP Window Machine(服务器和RMI注册表):192.168.56.1

IP Ubuntu计算机(客户端和HTTP服务器):192.168.1.70

这是服务器类:

package Esempio1;


import java.rmi.Naming;
import java.security.Policy;



public class ServerAdd {


    public ServerAdd() {
        try {

            System.setProperty("java.rmi.server.hostname","192.168.56.1");
            System.setProperty("java.rmi.server.useCodebaseOnly","false");
            System.setProperty("java.security.policy","policy.txt");
            System.setProperty("java.rmi.server.codebase","http://192.168.1.70/Root/");
            System.out.println(System.getProperty("java.rmi.server.codebase"));
            System.out.println(System.getProperty("java.rmi.server.useCodebaseOnly"));
            System.setSecurityManager(new SecurityManager());
            RemoteAdd a = new ImpAdd();
            Naming.rebind("add", a);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main (String[] args) {
        new ServerAdd();
    }

}

这是客户端:

package Esempio1;

import java.rmi.Naming;



public class ClientAdd {


    public ClientAdd() {
        try {

            System.setProperty("java.security.policy","policy.txt");
            System.setProperty("java.rmi.server.useCodebaseOnly","false");
            System.out.println(System.getProperty("java.rmi.server.useCodebaseOnly"));
            System.out.println(System.getProperty("java.security.policy"));

            if(System.getSecurityManager()==null) {
                System.setSecurityManager(new SecurityManager());

            }

            RemoteAdd a = (RemoteAdd) Naming.lookup("rmi://192.168.56.1:1099/add");

            Integer x = new Integer(8);
            Integer y = new Integer(2);
            Integer c = a.sommaIng(x, y);

            System.out.print(c.getClass()+" "+c);
        } catch (Exception e) {

            System.out.println(e);
        }
    }

    public static void main (String[] args) {
        new ClientAdd();
    }

}


我首先这样发送RMI注册中心:

start rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false

然后我将服务器与注册表运行在同一台计算机上,并且运行平稳...

最后,我在Ubuntu机器上运行客户端,然后返回此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Esempio1/RemoteAdd
        at Esempio1.ClientAdd.<init>(ClientAdd.java:23)
        at Esempio1.ClientAdd.main(ClientAdd.java:37)
Caused by: java.lang.ClassNotFoundException: Esempio1.RemoteAdd
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

这也是使用的策略文件:

grant
{
    permission java.security.AllPermission ;
};

HTTP Server通过Apache2服务在Ubuntu上运行,并包含一个内部带有Esempio1的Root文件夹以及所有.class文件(192.168.1.70/Root/Esempio1)

有人知道我错了吗?

0 个答案:

没有答案