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

 
  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;
    }
}
 
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值