URLClassLoader 类学习

 

使用该类能够加载任意位置的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);
		}
		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值