zoukankan      html  css  js  c++  java
  • [转]动态加载jar文件

    本文转自:http://zhongzhao-2008.iteye.com/blog/201487

    初学Java时教科书告诉我们,若要运行Java程序,必需为其设置环境变量。有时候为了实现某种功能(例如动态加载Class)我们需要在程序中来实现这个功能。我们经常使用的Eclipse就是很好的例子,它的所有插件的.jar文件都是放在plugin目录下的,如何加载这些包并使用其中的类成了一个棘手的问题。(关于Eclipse的类加载机制,大家可以参考《Contributing to Eclipse》p54)

    下面我们就来看看,在程序中如何实现这个功能。
    1、建立一个工程,并为其增加一个 Interface,命名为ActionInterface.java。该接口为使用者公开了一个方法,如下:

    程序代码
    package org.junesky.classPathTest;
    public interface ActionInterface {
        public String action();
    }

    编译后,将该工程打包为.jar文件。
    打包方法:
    在包的顶级目录(也就是例子中的org所在的目录)建立一个名为MANIFEST.MF的文件,键入:Manifest-Version: 1.0保存。
    在ms-doc窗口中键如命令:jar cvfm classLoader.jar MANIFEST.MF org/
    其中classLoader.jar是打包后的文件名,org/是path。

    2、建立一个新工程,将classLoader.jar导入,新建class文件MyAction并实现ActionInterface。如下:

    程序代码
    package org.junesky. MyAction;
    import org.junesky.classPathTest. ActionInterface;
    public class MyAction implements ActionInterface {
        public String action() {
            // TODO Auto-generated method stub
            return "------------------ok";
        }
    }

    编译后打包为classTest.jar。

    3、新建一工程,将classLoader.jar和classTest.jar导入,新建class文件MyClassLoader.java并继承URLClassLoader。如下:

    程序代码
    package org.junesky.classPathTest;

    import   java.net.URL;
    import   java.net.URLClassLoader;

    import org.junesky.classPathTest. ActionInterface;

    public class ClassPathTest extends URLClassLoader {
        public ClassPathTest(URL url) {
            super(new URL[] { url });
        }

        public static Object test(String str , String cls) {
            try {
                URL url = new URL(str);
                ClassPathTest t = new ClassPathTest(url);

    Class c = t.findClass(cls);
    ActionInterface action = (ActionInterface)c.newInstance();

    return action.action();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return "";
        }

        public static void main(String[] args) {
            try {
              
                System.out.println(
                    ClassPathTest.test("file:D:/JavaTest/ClassTest2/lib/ MyAction.jar",
                            "org.junesky. MyAction. MyAction "));

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    编译通过执行,会看到控制台上打印出:
    ------------------ok

    下面我们来看看,这个程序到底做了什么。首先,我们创建了一个接口ActionInterface,在利用URLClassLoader. findClass后,以这个接口创建对象的实例。在第二个jar文件中,我们创建了MyAction.java并实现了ActionInterface,在方法action中我们返回了一个字符串。接下来,我们利用URLClassLoader加载D:/JavaTest/ClassTest2/lib/ MyAction.jar,并以findClass查找并加载org.junesky. MyAction. MyAction类,为得到的Class生成实例:

    Class c = t.findClass(cls);  // 加载类
    ActionInterface action = (ActionInterface)c.newInstance();  // 生成对象实例

    最后调用ActionInterface中声明的action方法打印出MyAction中返回的字符串。

    以上代码仅仅是Java Class加载的使用方法的简单说明,在真正开发项目过程序中,往往还需要考虑更多的问题,以后我们再继续说明。

  • 相关阅读:
    函数【二】嵌套/作用域/名称空间
    内核模块加载错误 “Invalid module format” 解决办法
    linux oops调试
    linux 工具
    makefile 嵌套
    scheduling while atomic 出现的错误
    Windows下VSCode编译调试c/c++
    window markdown 编辑器
    linux 比较命令
    openwrt 加入nand flash的支持
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2563040.html
Copyright © 2011-2022 走看看