Dynamically load jar files

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">对于做客户端,最烦恼的事情就是。今天升级以后,明天客户反响了一大堆的问题,然后要想解决 就必须再次升级客户端。</span>

1. I was in a hurry when upgrading, and there were bugs when the code was written (mostly due to the occurrence of null pointers, or wrong conditions, or other).

2. The conditions we assume do not meet the current software environment.

However, users are particularly disgusted with frequent upgrades, and have always wondered whether they can put the code on the server side, like the WEB. If there is an error, I only need to update the code of the client, and the client can browse normally again.

Actually there are two solutions, (for android)

1. Silently upgrade the android client The so-called silent installation, but this is difficult to achieve (for most android machines, there are also permission restrictions)

2. My idea is to define a series of interfaces on the client side before upgrading the code that I think may have problems, and put the implementation on the server. Then download your own jar locally through the network. Load the jar file through the dynamic loading mechanism of jar, and call our implementation class through the reflection mechanism of java.

Okay, not much to say, I am interested in dynamically loading jar files and using generics to get corresponding instances


package com.cyl.utils;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;



/**
 * 动态加载jar 文件   通过本地文件  或者  网络文件
 * @author cyl
 *
 * 2016-2-12
 */
public class LoadJarFile {
	//本地文件地址
	private String localPath;
	//自定义的类加载器
	private static MyClassLoader classLoader;
	public LoadJarFile(String localPath) {
		super();
		this.localPath = localPath;
		init();
	}
	/**
	 * 初始化classloader
	 */
	private void init() {
		if(classLoader != null){
			return;
		}
		try {
			URL[] urls = new URL[] {};
			classLoader = new MyClassLoader(urls, Thread.currentThread().getContextClassLoader());
			classLoader.addJar(new File(localPath).toURI().toURL());
		} catch (Exception e) {
		}
	}
	
	/**
	 * 通过类名得到对应的类的实例
	 * @param clz        得到对象的对应类型
	 * @param className  类的全名
	 * @return   
	 */
	public <T> T getClassByJar(Class<T> clz,String className){
		try {
			if(classLoader!=null){
				Class<?> class1 = classLoader.loadClass(className);
				if(class1 != null){
					Object o = class1.newInstance();
					return (T) o;
				}
			}
		} catch (Exception e) {
			//这里没有做太多的处理   如:类名字出错了,以及  jar 文件的地址出错了
			return null;
		}
		return null;
	}
	/**
	 * 自定义的classloader
	 * @author cyl
	 *
	 * 2016-2-12
	 */
	static class MyClassLoader extends URLClassLoader {

	        public MyClassLoader(URL[] urls) {
	            super(urls);
	        }

	        public MyClassLoader(URL[] urls, ClassLoader parent) {
	            super(urls, parent);
	        }

	        public void addJar(URL url) {
	            this.addURL(url);
	        }

	    }
	
}


If you have any questions, you can discuss them together!

Guess you like

Origin blog.csdn.net/tiandiyinghun/article/details/50655539
Recommended