用URLClassLoader 在java运行期间jar文件动态载入类

时间:2021-06-04 19:38:24
 
  URLClassLoaderUtil jar文件载入工具类
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
 
public class URLClassLoaderUtil {
    URLClassLoader classLoader = null ; // URLClassLoader 类载入器
    private String jarFileName ;
    private boolean isFile = true ;
    List<String> jars = new ArrayList<String>(0);
 
    public URLClassLoaderUtil(String jarFileName) {
       this .setJarFileName(jarFileName);
       this .inti();
    }
 
    public URLClassLoaderUtil(String jarFileName, boolean isFile) {
       this .setJarFileName(jarFileName);
       this .setFile(isFile);
       this .inti();
    }
 
    /**
      * 初始化,读取文件信息,并将 jar 文件路径加入到 classpath
      */
    private void inti() {
       // 添加 jar 文件路径到 classpath
       if ( this . isFile ) {
           File f = new File( jarFileName );
           addPath(f.toURI().toString());
           jars .add(f.getAbsolutePath());
       } else {
           ReadJarFile df = new ReadJarFile( jarFileName , new String[] { "jar" ,
                  "zip" });
           this . jars = df.getFiles();
           List<String> jarURLs = df.getFilesURL();
           Object[] o = jarURLs.toArray();
           addPath(o);
       }
    }
 
    /**
      * 回叫方法, class 操作
      *
      * @param callBack
      */
    public void callBack(ClassCallBack callBack) {
       for (String s : this . jars ) {
           loadClass(s, callBack);
       }
    }
 
    /**
      * 添加单个 jar 路径到 classpath
      *
      * @param jarURL
      */
    private void addPath(String jarURL) {
       try {
           classLoader = new URLClassLoader( new URL[] { new URL(jarURL) });
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }
    }
 
    /**
      * 添加 jar 路径到 classpath
      *
      * @param jarURLs
      */
    private void addPath(Object[] jarURLs) {
       URL[] urls = new URL[jarURLs. length ];
       for ( int i = 0; i < jarURLs. length ; i++) {
           try {
              urls[i] = new URL(jarURLs[i].toString());
           } catch (MalformedURLException e) {
              e.printStackTrace();
           }
       }
       classLoader = new URLClassLoader(urls);
    }
 
    /**
      * 动态载入 class
      *
      * @param jarFileName
      * @param callBack
      */
    private void loadClass(String jarFileName, ClassCallBack callBack) {
       JarFile jarFile = null ;
       try {
           jarFile = new JarFile(jarFileName);
       } catch (IOException e) {
           e.printStackTrace();
       }
       Enumeration<JarEntry> en = jarFile.entries();
       while (en.hasMoreElements()) {
           JarEntry je = en.nextElement();
           String name = je.getName();
           String s5 = name.replace( '/' , '.' );
           if (s5.lastIndexOf( ".class" ) > 0) {
              String className = je.getName().substring(0,
                     je.getName().length() - ".class" .length()).replace( '/' ,
                     '.' );
              Class c = null ;
              try {
                  c = this . classLoader .loadClass(className);
                  System. out .println(className);
              } catch (ClassNotFoundException e) {
                  System. out .println( "NO CLASS: " + className);
                  // continue;
              } catch (NoClassDefFoundError e) {
                  System. out .println( "NO CLASS: " + className);
                  // continue;
              }
              callBack.operate(c);
           }
       }
    }
 
    public String getJarFileName() {
       return jarFileName ;
    }
 
    public void setJarFileName(String jarFileName) {
       this . jarFileName = jarFileName;
    }
 
    public boolean isFile() {
       return isFile ;
    }
 
    public URLClassLoader getClassLoader() {
       return classLoader ;
    }
 
    public void setClassLoader(URLClassLoader classLoader) {
       this . classLoader = classLoader;
    }
 
    public void setFile( boolean isFile) {
       this . isFile = isFile;
    }
}