使用该类能够加载任意位置的jar,可以实现“动态加载jar包”的目的
package com.hb;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.junit.Test;
public class URLClassLoaderDemo {
/**
* 根据文件File对象获取转为URL对象
*/
@Test
public void getUrlByFile() {
String path = "d:/hb.jar";
File file = new File(path);
try {
// 打印结果 file:/d:/hb.jar
System.out.println(file.toURI().toURL());
// file.toURL这个方法已经不赞成使用了
// System.out.println(file.toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
/**
* 使用ClassLoader类来获取文件流,即inputStream对象
*/
@Test
public void getStreamByClassLoader() {
URLClassLoaderDemo currentObj = new URLClassLoaderDemo();
// 从用来加载类的搜索路径打开具有指定名称的资源,以读取该资源。此方法通过系统类加载器(参见
// getSystemClassLoader())来查找资源
// AppProperty.xml文件在src目录下面
InputStream is = URLClassLoaderDemo.class.getClassLoader()
.getResourceAsStream("AppProperty.xml");
if (is != null) {
System.out.println(is);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有获取到InputStream对象");
}
}
/**
* 动态的获取jar包中的对象,初始化的时候指明jar包的位置
*/
@Test
public void getObjectFromJar() {
String path = "d:/hb.jar";
File file = new File(path);
URL[] urls;
try {
//动态加载jar包的引用
urls = new URL[] {file.toURI().toURL()};
URLClassLoader urlClassLoader = URLClassLoader.newInstance(urls);
Class clazz = urlClassLoader.loadClass("com.hb.JNotePadUI");
//根据类的全名称获取对象
JNotePadUI obj = (JNotePadUI)clazz.newInstance();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 动态的获取jar包中的对象,在代码运行的时候动态的加载jar包,利用这种方式我们可以动态的解析插件的入口方法
*/
@Test
public void getObjectFromJarDelay() {
String path = "d:/hb.jar";
File file = new File(path);
URL[] urls;
try {
//动态加载jar包的引用
urls = new URL[] {};
MyUrlClassLoader urlClassLoader = new MyUrlClassLoader(urls);
urlClassLoader.addUrl(file.toURI().toURL());
//根据类的全名称获取对象
Class clazz = urlClassLoader.loadClass("com.hb.JNotePadUI");
JNotePadUI obj = (JNotePadUI)clazz.newInstance();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/**
* 之定义一个类继承URLClassLoader类,目的是为了实现其addURL方法
* @author Administrator
*
*/
public class MyUrlClassLoader extends URLClassLoader{
public MyUrlClassLoader(URL[] urls) {
super(urls);
}
public MyUrlClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
/**
* 因为URLClassLoader的addURL方法是protected修饰了,只能被其子类或者是同一个类中的某个方法或成员变量在包中都可见
* 因此需要自定义一个子类继承URLClassLoader类,复写其addURL方法
* @param url
*/
public void addUrl(URL url){
super.addURL(url);
}
}
}