Java 动态加载Jar包,并使用

  将一个jar包动态加载到java虚拟机中

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import org.apache.commons.lang3.reflect.FieldUtils;

public class JarTools {

    protected static Method addURL = null ;
    static{
        try {
            addURL = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }) ; 
            addURL.setAccessible(true);
        } catch (Exception e) {
        }
    }

    /**
     * 动态加载Jar包到内存中
     * */
    public static Object loadJar(String jarFile, String className) {
        try {
            File file = new File( jarFile ); 
            if (!file.exists()) {
                throw new RuntimeException(jarFile + "不存在");
            }
            addURL.invoke(ClassLoader.getSystemClassLoader(), new Object[] { file.toURI().toURL() }); 
            return Class.forName( className ,false , ClassLoader.getSystemClassLoader() ).newInstance(); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        Object loadJar = loadJar("C:/commons-io-2.4.jar", "org.apache.commons.io.Charsets") ;
        Field field = loadJar.getClass().getField("UTF_8"); 
        System.out.println(FieldUtils.readDeclaredStaticField(loadJar.getClass(), field.getName()));
    }
}

猜你喜欢

转载自hpgary.iteye.com/blog/2379766