动态加载jar包,在实际开发中经常会需要用到,尤其涉及平台和业务的关系的时候,业务逻辑部分可以独立出去交给业务方管理,业务方只需要提供jar包,就能在平台上运行。
下面通过一个实例来直观演示:
第一:定义一个抽象类 AbstractAction (稍后换成接口的实例)
- package com.java.loader;
- public abstract class AbstractAction {
- public abstract String action();
- }
第二:写个实体类继承一下 TestAction
- package com.java.jarloader;
- import com.java.loader.AbstractAction;
- public class TestAction extends AbstractAction{
- public String action() {
- System.out.println("I am working ! ");
- return "this ActionTest class";
- }
- }
第三:将TestAction所在的包导出成jar包的方式,eclipse中直接export即可,放到指定目录,此处放在
- D:\jarload\test.jar
放好后,删除TestAction文件和package。
第四:写个main函数测试下 TestMain (这里比较无聊,写了个从文件读的方式获取jar路劲,路径就是上面提到的jar所在的位置)
- package com.java.main;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.net.URL;
- import java.net.URLClassLoader;
- import com.java.loader.AbstractAction;
- import com.java.loader.AbstractAction;
- /**
- *
- * 两种方式
- * @author minggang.wumg
- *
- */
- public class TestMain {
- public static void main(String[] args) {
- try {
- //第一种 配置成文件格式
- File file = new File("D:\\jarload\\test.txt");
- BufferedReader in = new BufferedReader(new FileReader(file));
- String s = new String();
- while ((s = in.readLine()) != null) {
- URL url = new URL(s);
- s = null;
- URLClassLoader myClassLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread()
- .getContextClassLoader());
- Class<? extends AbstractAction> myClass = (Class<? extends AbstractAction>) myClassLoader.loadClass("com.java.jarloader.TestAction");
- AbstractAction action = (AbstractAction) myClass.newInstance();
- String str = action.action();
- System.out.println(str);
- //第二种
- URL url1 = new URL("file:D:/jarload/test.jar");
- URLClassLoader myClassLoader1 = new URLClassLoader(new URL[] { url1 }, Thread.currentThread()
- .getContextClassLoader());
- Class<?> myClass1 = myClassLoader1.loadClass("com.java.jarloader.TestAction");
- AbstractAction action1 = (AbstractAction) myClass1.newInstance();
- String str1 = action1.action();
- System.out.println(str1);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
第五:运行结果:
这是能完成运行的!
下面我们来改写下:
第一:将抽象类改成接口的形式 InterfaceAction
- package com.java.loader;
- public interface InterfaceAction {
- public String action();
- }
第二:改写下实体类,实现接口 TestAction
- package com.java.jarloader;
- import com.java.loader.InterfaceAction;
- public class TestAction implements InterfaceAction{
- @Override
- public String action() {
- System.out.println("I am working ! ");
- return "this ActionTest class";
- }
- }
第三步相同。
第四步:稍作修改 TestMain
- package com.java.main;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.net.URL;
- import java.net.URLClassLoader;
- import javax.swing.AbstractAction;
- import com.java.loader.InterfaceAction;
- /**
- *
- * 两种方式
- * @author minggang.wumg
- *
- */
- public class TestMain {
- public static void main(String[] args) {
- try {
- //第一种 配置成文件格式
- File file = new File("D:\\jarload\\test.txt");
- BufferedReader in = new BufferedReader(new FileReader(file));
- String s = new String();
- while ((s = in.readLine()) != null) {
- URL url = new URL(s);
- s = null;
- URLClassLoader myClassLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread()
- .getContextClassLoader());
- Class<?> myClass = (Class<?>) myClassLoader.loadClass("com.java.jarloader.TestAction");
- InterfaceAction action = (InterfaceAction) myClass.newInstance();
- String str = action.action();
- System.out.println(str);
- //第二种
- URL url1 = new URL("file:D:/jarload/test.jar");
- URLClassLoader myClassLoader1 = new URLClassLoader(new URL[] { url1 }, Thread.currentThread()
- .getContextClassLoader());
- Class<?> myClass1 = myClassLoader1.loadClass("com.java.jarloader.TestAction");
- InterfaceAction action1 = (InterfaceAction) myClass1.newInstance();
- String str1 = action1.action();
- System.out.println(str1);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
第五:运行结果相同。
转自http://blog.csdn.net/wawmg/article/details/17961815