加载并获取jar包中某个类的某个方法

来源:互联网 发布:触摸屏设备 软件开发 编辑:程序博客网 时间:2024/05/16 00:54
public class JavaTest {

 
public static void main(String[] args) {
        try {
            // File file = new File("file:///e:\\taskJarTest.jar");
            // URL url = file.toURI().toURL();
            URL url = new URL("file:///e:\\taskJarTest.jar");
            URLClassLoader loader = new URLClassLoader(new URL[] { url });
            Class tidyClazz = loader.loadClass("类全名");
            Object ins = tidyClazz.newInstance();
            System.out.println(tidyClazz);
            Method m = tidyClazz.getDeclaredMethod("方法名");
            m.setAccessible(true);
            m.invoke(ins);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}

0 0
原创粉丝点击