当前位置: 首页 > 工具软件 > Little Loader > 使用案例 >

Java Loader

孙经艺
2023-12-01
package com.java.user.loader;


import java.io.File;
import java.lang.reflect.Method;
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;


/**
 * 
 * @summary 加载用户自定义的jar的ClassLoader
 * @author 周正德(zhouzhengde)
 * @date 2014年12月28日
 */
public class UserClassLoader {
/** URLClassLoader的addURL方法 */
private static Method addURL = initAddMethod();


private static URLClassLoader system = (URLClassLoader) ClassLoader.getSystemClassLoader();


/** 初始化方法 */
private static final Method initAddMethod() {
Method add = null;
try {
add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
add.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
return add;
}


/**
* 循环遍历目录,找出所有的JAR包
*/
private static final void loopFiles(File file, List<File> files) {
if (file.isDirectory()) {
File[] tmps = file.listFiles();
for (File tmp : tmps) {
loopFiles(tmp, files);
}
} else {
if (file.getAbsolutePath().endsWith(".jar") || file.getAbsolutePath().endsWith(".zip")) {
files.add(file);
}
}
}


/**
* <pre>
* 加载JAR文件
* </pre>

* @param file
*/
public static final void loadJarFile(File file) throws Exception {
addURL.invoke(system, new Object[] { file.toURI().toURL() });
}


/**
* <pre>
* 加载JAR文件
* </pre>

* @param file
*/
public static final void loadJarFile(String fileName) throws Exception {
File file = new File(fileName);
addURL.invoke(system, new Object[] { file.toURI().toURL() });
System.out.println("加载JAR包:" + file.getAbsolutePath());
}


/**
* <pre>
* 从一个目录加载所有JAR文件
* </pre>

* @param path
*/
public static final void loadJarPath(String path) throws Exception {
List<File> files = new ArrayList<File>();
File lib = new File(path);
loopFiles(lib, files);
for (File file : files) {
loadJarFile(file);
}
}


/**
* 从指定的Jar文件中获取所有类的类名

* @param jarName
* @return
* @throws Exception
*/
public static List<String> getClassInJar(String jarName) throws Exception {
List<String> list = new ArrayList<String>();
JarFile file = new JarFile(jarName);
Enumeration<JarEntry> en = file.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('/', '.');
list.add(className);
}
}
file.close();
return list;
}


/**
* 从指定的Jar文件中获取所有类的类名

* @param jarName
* @return
* @throws Exception
*/
public static List<String> getClassInJarPath(String jarPath) throws Exception {
List<String> list = new ArrayList<String>();
File file = new File(jarPath);
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
for (File item : listFiles) {
list.addAll(getClassInJar(item.getAbsolutePath()));
}
} else {
list = getClassInJar(file.getAbsolutePath());
}
return list;
}
}
 类似资料:

相关阅读

相关文章

相关问答