自定义的ClassLoader动态外部jar文件,但加载不到jar里的配置文件
编写了一个MyClassLoader继承于URLClassLoader,用于其他程序的ar文件并运行。
但加载一个使用了spring框架的程序,运行时spring在读取applicationContext.xml时会报出
读取不到声明的xsd文件如http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
spring的jar里面都有相应的xsd文件,如果网络不通会直接读取jar里的xsd文件
下面是MyClassLoader的代码
public class MyClassLoader extends URLClassLoader {
public MyClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
super(urls, parent, factory);
// TODO Auto-generated constructor stub
}
public MyClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
// TODO Auto-generated constructor stub
}
public MyClassLoader(URL[] urls) {
super(urls);
// TODO Auto-generated constructor stub
}
public void runClass(String className, String methodName) throws ClassNotFoundException, SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException, InstantiationException{
Class classType = loadClass(className);
Object obj = classType.newInstance();
Method method = classType.getDeclaredMethod(methodName, null);
method.invoke(obj, null);
}
}
下面是调用的代码
URL[] urls = getURL(); //返回其他程序的jar的地址
MyClassLoader loader = new MyClassLoader(urls, Thread.currentThread().getContextClassLoader());
try{
loader.runClass(className, methodName);
}catch(Exception e){
e.printStackTrace();
}
但,我在网过找到一个直接把其他程序的jar加载到SystemClassLoader源代码把其添加到项目代替MyClassLoader,并运行却不会出任何错误
下面是其代码
public final class ClassLoaderUtils {
/** URLClassLoader的addURL方法 */
private static Method addURL = initAddMethod();
/** 初始化方法 */
private static final Method initAddMethod() {
try {
Method add = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[] { URL.class });
add.setAccessible(true);
return add;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static URLClassLoader system = (URLClassLoader) ClassLoader
.getSystemClassLoader();
/**
* 循环遍历目录,找出所有的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) {
try {
addURL.invoke(system, new Object[] { file.toURI().toURL() });
System.out.println("加载JAR包:" + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 这是我自己添加的方法,用于启动其他程序
* @param className 类名
* @param methodName 其他程序的运行接口
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
*/
public static void runClass(String className, String methodName) throws ClassNotFoundException, SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException, InstantiationException{
Class classType = Class.forName(className);
Object obj = classType.newInstance();
Method method = classType.getDeclaredMethod(methodName, null);
method.invoke(obj, null);
}
/**
* <pre>
* 从一个目录加载所有JAR文件
* </pre>
*
* @param path
*/
public static final void loadJarPath(String path) {
List<File> files = new ArrayList<File>();
File lib = new File(path);
loopFiles(lib, files);
for (File file : files) {
loadJarFile(file);
}
}
}
调用
ClassLoaderUtils.loadJarPath(filePath); //根据jar文件夹路径,遍历加载jar文件
try {
ClassLoaderUtils.runClass(className, methodName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请问MyClassLoader和ClassLoaderUtils有什么差别,MyClassLoader加载的spring jar文件为什么会找不到jar里的xsd文件