简体   繁体   中英

How to load methods from a jar without loading class?

I am trying to read a Jar file which contains few classes and has methods defined in it. I want to get all the declared methods from all the classes without loading individual classes. is it possible?

Here's how I am loading jar file and method(Using class name as of now).

File urlclasspath = new File(newJarPath);
        URL urlarray[] = new URL[1];
        urlarray[0] = urlclasspath.toURI().toURL();
        ClassLoader loader = new ClassLoader(urlarray);
        Class<?> myclass = loader
                .loadClass($classname$);

        Object obj = myclass.newInstance();
        Method add = myclass.getMethod("add", new Class[] { Integer.TYPE,
                Integer.TYPE });
        add.invoke(obj,
                new Object[] { new Integer(var1), new Integer(var2) });

        if (obj == null) {
            System.out
                    .println("FAIL: Could not create an instance of the loaded class");
            }

Thanks in advance

类是具有in。的所有方法的容器。因此,要访问某个方法,必须首先加载该类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM