java动态加载jar包,并运行其中的类和方法

动态加载jar包,在实际开发中经常会需要用到,尤其涉及平台和业务的关系的时候,业务逻辑部分可以独立出去交给业务方管理,业务方只需要提供jar包,就能在平台上运行。

下面通过一个实例来直观演示:

第一:定义一个抽象类 AbstractAction (稍后换成接口的实例)

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.java.loader;  
  2.   
  3. public abstract class AbstractAction {  
  4.     public abstract String action();  
  5. }  

第二:写个实体类继承一下 TestAction

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.java.jarloader;  
  2.   
  3. import com.java.loader.AbstractAction;  
  4.   
  5. public class TestAction extends AbstractAction{  
  6.     public String action() {  
  7.         System.out.println("I am working ! ");  
  8.         return "this ActionTest class";  
  9.     }  
  10. }  

第三:将TestAction所在的包导出成jar包的方式,eclipse中直接export即可,放到指定目录,此处放在

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. D:jarload est.jar  


放好后,删除TestAction文件和package。

第四:写个main函数测试下 TestMain (这里比较无聊,写了个从文件读的方式获取jar路劲,路径就是上面提到的jar所在的位置)

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.java.main;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.net.URL;  
  7. import java.net.URLClassLoader;  
  8.   
  9. import com.java.loader.AbstractAction;  
  10. import com.java.loader.AbstractAction;  
  11.   
  12.   
  13. /** 
  14.  *  
  15.  * 两种方式 
  16.  * @author minggang.wumg 
  17.  * 
  18.  */  
  19.   
  20. public class TestMain {  
  21.   
  22.     public static void main(String[] args) {  
  23.         try {  
  24.             //第一种  配置成文件格式  
  25.             File file = new File("D:\jarload\test.txt");  
  26.             BufferedReader in = new BufferedReader(new FileReader(file));  
  27.             String s = new String();  
  28.             while ((s = in.readLine()) != null) {  
  29.   
  30.                 URL url = new URL(s);  
  31.                 s = null;  
  32.                   
  33.                 URLClassLoader myClassLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread()  
  34.                         .getContextClassLoader());  
  35.                 Class<? extends AbstractAction> myClass = (Class<? extends AbstractAction>) myClassLoader.loadClass("com.java.jarloader.TestAction");  
  36.                 AbstractAction action = (AbstractAction) myClass.newInstance();  
  37.                 String str = action.action();  
  38.                 System.out.println(str);  
  39.                   
  40.                 //第二种  
  41.                 URL url1 = new URL("file:D:/jarload/test.jar");  
  42.                 URLClassLoader myClassLoader1 = new URLClassLoader(new URL[] { url1 }, Thread.currentThread()  
  43.                         .getContextClassLoader());  
  44.                 Class<?> myClass1 = myClassLoader1.loadClass("com.java.jarloader.TestAction");  
  45.                 AbstractAction action1 = (AbstractAction) myClass1.newInstance();  
  46.                 String str1 = action1.action();  
  47.                 System.out.println(str1);  
  48.             }  
  49.         } catch (Exception e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.   
  54. }  


第五:运行结果:

这是能完成运行的!

下面我们来改写下:

第一:将抽象类改成接口的形式  InterfaceAction 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.java.loader;  
  2.   
  3. public interface InterfaceAction {  
  4.     public String action();  
  5. }  

第二:改写下实体类,实现接口 TestAction

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.java.jarloader;  
  2.   
  3. import com.java.loader.InterfaceAction;  
  4.   
  5. public class TestAction implements InterfaceAction{  
  6.   
  7.     @Override  
  8.     public String action() {  
  9.         System.out.println("I am working ! ");  
  10.         return "this ActionTest class";  
  11.     }  
  12.   
  13. }  


第三步相同。

第四步:稍作修改 TestMain

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.java.main;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.net.URL;  
  7. import java.net.URLClassLoader;  
  8.   
  9. import javax.swing.AbstractAction;  
  10.   
  11. import com.java.loader.InterfaceAction;  
  12.   
  13.   
  14.   
  15.   
  16. /** 
  17.  *  
  18.  * 两种方式 
  19.  * @author minggang.wumg 
  20.  * 
  21.  */  
  22.   
  23. public class TestMain {  
  24.   
  25.     public static void main(String[] args) {  
  26.         try {  
  27.             //第一种  配置成文件格式  
  28.             File file = new File("D:\jarload\test.txt");  
  29.             BufferedReader in = new BufferedReader(new FileReader(file));  
  30.             String s = new String();  
  31.             while ((s = in.readLine()) != null) {  
  32.   
  33.                 URL url = new URL(s);  
  34.                 s = null;  
  35.                   
  36.                 URLClassLoader myClassLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread()  
  37.                         .getContextClassLoader());  
  38.                 Class<?> myClass = (Class<?>) myClassLoader.loadClass("com.java.jarloader.TestAction");  
  39.                 InterfaceAction action = (InterfaceAction) myClass.newInstance();  
  40.                 String str = action.action();  
  41.                 System.out.println(str);  
  42.                   
  43.                 //第二种  
  44.                 URL url1 = new URL("file:D:/jarload/test.jar");  
  45.                 URLClassLoader myClassLoader1 = new URLClassLoader(new URL[] { url1 }, Thread.currentThread()  
  46.                         .getContextClassLoader());  
  47.                 Class<?> myClass1 = myClassLoader1.loadClass("com.java.jarloader.TestAction");  
  48.                 InterfaceAction action1 = (InterfaceAction) myClass1.newInstance();  
  49.                 String str1 = action1.action();  
  50.                 System.out.println(str1);  
  51.             }  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.   
  57. }  


第五:运行结果相同。

转自http://blog.csdn.net/wawmg/article/details/17961815

原文地址:https://www.cnblogs.com/Bonker/p/5445734.html