Java Loader

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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值