URLClassLoader

Thread.currentThread().getContextClassLoader();


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;

publicclass URLClassLoaderUtil {
    URLClassLoader classLoader = null;// URLClassLoader类载入器
    private String jarFileName;
    privatebooleanisFile = 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
     */
    privatevoid 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操作
     *
     *@paramcallBack
     */
    publicvoid callBack(ClassCallBack callBack) {
       for (String s : this.jars) {
           loadClass(s, callBack);
       }
    }

    /**
     *添加单个jar路径到classpath
     *
     *@paramjarURL
     */
    privatevoid addPath(String jarURL) {
       try {
           classLoader = new URLClassLoader(new URL[] { new URL(jarURL) });
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }
    }

    /**
     *添加jar路径到classpath
     *
     *@paramjarURLs
     */
    privatevoid 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
     *
     *@paramjarFileName
     *@paramcallBack
     */
    privatevoid 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() {
       returnjarFileName;
    }

    publicvoid setJarFileName(String jarFileName) {
       this.jarFileName = jarFileName;
    }

    publicboolean isFile() {
       returnisFile;
    }

    public URLClassLoader getClassLoader() {
       returnclassLoader;
    }

    publicvoid setClassLoader(URLClassLoader classLoader) {
       this.classLoader = classLoader;
    }

    publicvoid setFile(boolean isFile) {
       this.isFile = isFile;
    }
}

你可能感兴趣的:(java,C++,c,.net,C#)