Reputation: 53
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
Upvotes: 0
Views: 284
Reputation: 9946
Class is the container for all the methods with in. so in order to reach to a method you must load the class first.
Upvotes: 1