自定义的ClassLoader动态外部jar文件,但加载不到jar里的配置文件

xin_feng_08 2011-07-27 05:22:37
编写了一个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文件
...全文
447 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xin_feng_08 2011-07-28
  • 打赏
  • 举报
回复
怎么没人回复, T_T
xin_feng_08 2011-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 p_rince 的回复:]

确定路径没有错误?
[/Quote]
没有
shawn.bug 2011-07-27
  • 打赏
  • 举报
回复
确定路径没有错误?

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧