자바 jar 의 동적 로드 와 방출

1247 단어
package com.zeus;

import sun.misc.ClassLoaderUtil;

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

/**
 * java jar        
 */
public class TestMain {


    public static void main(String[] args) throws MalformedURLException, Exception {

        System.out.println(new File(".").getAbsolutePath());
        URLClassLoader urlClassLoader=new URLClassLoader(new URL[]{new URL("file:C:\\Users\\Administrator\\IdeaProjects\\test\\data.jar")});
       Class classStudentServiceImpl=  urlClassLoader.loadClass("StudentServiceImpl");
       Method method= classStudentServiceImpl.getMethod("getName",new Class[]{});
        Constructor localConstructor = classStudentServiceImpl.getConstructor(new Class[] {});
        Object instance = localConstructor.newInstance(new Object[] {});
       Object ret= method.invoke(instance);
        System.out.println(ret);

        ClassLoaderUtil.releaseLoader(urlClassLoader);
        while(true)
        {
            Thread.sleep(1000);
            System.out.println("run");
        }


    }
}

좋은 웹페이지 즐겨찾기