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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值