java中动态调用jar包

来源:互联网 发布:seafile linux 编辑:程序博客网 时间:2024/04/29 00:08

步骤一、拷贝下面这个类到你的工程,我写的,可以进一步优化
package dmis;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.JarURLConnection;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;
import java.util.jar.Attributes;
import java.io.IOException;
import javax.swing.*;

/**
 * A class loader for loading jar files, both local and remote.
 */
class JarClassLoader extends URLClassLoader {
    private URL url;

    /**
     * Creates a new JarClassLoader for the specified url.
     *
     * @param url the url of the jar file
     */
    public JarClassLoader(URL url) {
        super(new URL[] { url });
        this.url = url;
    }

    /**
     * Returns the name of the jar file main class, or null if
     * no "Main-Class" manifest attributes was defined.
     */
    public String getMainClassName() throws IOException {
        URL u = new URL("jar", "", url + "!/");
        JarURLConnection uc = (JarURLConnection)u.openConnection();
        Attributes attr = uc.getMainAttributes();
        return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
    }

    /**
     * Invokes the application in this jar file given the name of the
     * main class and an array of arguments. The class must define a
     * static method "main" which takes an array of String arguemtns
     * and is of return type "void".
     *
     * @param name the name of the main class
     * @param args the arguments for the application
     * @exception ClassNotFoundException if the specified class could not
     *            be found
     * @exception NoSuchMethodException if the specified class does not
     *            contain a "main" method
     * @exception InvocationTargetException if the application raised an
     *            exception
     */
    public void invokeClass(String name, String[] args)
        throws ClassNotFoundException,
               NoSuchMethodException,
               InvocationTargetException
    {
        Class c = loadClass(name);
        Method m = c.getMethod("main", new Class[] { args.getClass() });
        m.setAccessible(true);
        int mods = m.getModifiers();
        if (m.getReturnType() != void.class || !Modifier.isStatic(mods) ||
            !Modifier.isPublic(mods)) {
            throw new NoSuchMethodException("main");
        }
        try {
            m.invoke(null, new Object[] { args });
        } catch (IllegalAccessException e) {
            // This should not happen, as we have disabled access checks
        }
    }

    public void invokeClass(String name)
         throws ClassNotFoundException,
                NoSuchMethodException,
                InvocationTargetException
     {
       Class c=loadClass(name);
       Method m=c.getMethod("main",null);
       m.setAccessible(true);
         try{
           m.invoke(null,null);
         }catch(IllegalAccessException e){
         }
        }

}

二、子模块(jar文件)写接口
    即在子模块的main class中多一个方法:public static main(){//这里启动整个子模块}
    然后把该子模块打包成jar文件(注意manifest文件中必须声明该jar 文件的main class)
三、动态调用子模块
    JarClassLoader cl=new JarClassLoader(url);//url指向你的子模块即jar文件的路径
    String name=cl.getMainClassName();
    cl.invokeClass(name);//OK!