Want each class method information from jar file.

Hi All,

I have to find each classes method information like its return type, argument etc. from jar file. Till now i got the class name from the jar. When i used reflection on class then its gives different method name which are not present in the class.

So what should i do?

See the code:

import java.io.*;

import java.lang.reflect.Method;

import java.util.*;

import java.util.jar.*;

public class JarDir

{

public static void main (String args[])throws IOException

{

if (args.length != 1)

{

System.out.println("Please provide a JAR filename");

System.exit(-1);

}

JarFile jarFile = new JarFile(args[0]);

Enumeration enum1 = jarFile.entries();

while (enum1.hasMoreElements())

{

process(enum1.nextElement());

}

}

private static void process(Object obj)

{

JarEntry entry = (JarEntry)obj;

String name = entry.getName();

if (name.endsWith(".class"))

{

Class c = obj.getClass();

Method[] theMethods = c.getMethods();

for (int i = 0; i < theMethods.length; i++) {

String methodString = theMethods.getName();

System.out.println("Name: " + methodString);

String returnString =

theMethods.getReturnType().getName();

System.out.println("Return Type: " + returnString);

Class[] parameterTypes = theMethods.getParameterTypes();

System.out.print("Parameter Types:");

for (int k = 0; k < parameterTypes.length; k ++) {

String parameterString = parameterTypes[k].getName();

System.out.print(" " + parameterString);

}

System.out.println();

}

}

long size = entry.getSize();

long compressedSize = entry.getCompressedSize();

System.out.println(name + "\t" + size + "\t" + compressedSize);

}

}

*************************************************************************

Cmd argument : java JarDir jar-path

*************************************************************************

please help me and thanx in advance

-bunty

[2192 byte] By [bunty_bargea] at [2007-11-26 17:20:17]
# 1

the class you are querying there is java.util.jar.JarEntry, not the class you've found inside the jar. if the class has already been loaded, you can get it using the fully-qualified (with package) name like this

String className = "however.you.get.the.name";

Class clazz = Class.forName(className);

if the class isn't loaded (the above fails) you need to get the URL of that class inside the jar, and load it yourself using a URLClassLoader

georgemca at 2007-7-8 23:48:16 > top of Java-index,Core,Core APIs...
# 2
Yes, first one means Class. forName() is not working.Can u explain more about URLClassLoader? If you dont mind can u explain how I load a class in URLClassLoader?Thanx in advance.-Bunty
bunty_bargea at 2007-7-8 23:48:16 > top of Java-index,Core,Core APIs...
# 3
1) get the URL to the class file you're interested in, and put it in an array of URLs2) get a URLClassLoader and load classURLClassLoader loader = URLClassLoader.newInstance(urls);Class clazz = loader.loadClass(classname);
georgemca at 2007-7-8 23:48:16 > top of Java-index,Core,Core APIs...
# 4

Hi georgemc .

I have used that but it gives exception :

java.lang.ClassNotFoundException: Hello

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at JarDir.load_class(JarDir.java:55)

at JarDir.process(JarDir.java:34)

at JarDir.main(JarDir.java:24)

Exception in thread "main" java.lang.NullPointerException

at JarDir.process(JarDir.java:35)

at JarDir.main(JarDir.java:24)

The code is :

***************************************

import java.io.*;

import java.lang.reflect.Method;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLClassLoader;

import java.util.*;

import java.util.jar.*;

public class JarDir

{

public static void main (String args[])throws IOException

{

/*if (args.length != 1)

{

System.out.println("Please provide a JAR filename");

System.exit(-1);

}*/

JarFile jarFile = new JarFile("F:\\ProxyGenerator\\ProxyGeneratorTool\\Hello.jar");

Enumeration enum1 = jarFile.entries();

while (enum1.hasMoreElements())

{

process(enum1.nextElement());

}

}

private static void process(Object obj)

{

JarEntry entry = (JarEntry)obj;

String name = entry.getName();

if (name.endsWith(".class"))

{

Class myClass = load_class("Hello");

System.out.println(myClass.getName());

Method[] theMethods = myClass.getDeclaredMethods();

for (int i = 0; i < theMethods.length; i++)

{

String methodString = theMethods.getName();

System.out.println("Name: " + methodString);

}

}

long size = entry.getSize();

long compressedSize = entry.getCompressedSize();

System.out.println(name + "\t" + size + "\t" + compressedSize);

}

private static Class load_class(String className)

{

try

{

File file = new File(".");

//ClassLoader loader = new URLClassLoader(new URL[] {file.toURL()});

ClassLoader loader = new URLClassLoader(new URL[] {file.toURL()});

return loader.loadClass(className);

}

catch (ClassNotFoundException e) { e.printStackTrace(); }

catch (MalformedURLException e) { e.printStackTrace(); }

return null;

}

}

****************************************************************************

If i am doing any mistake any where please let me know.

This is very important for me.

Thanks.

bunty_bargea at 2007-7-8 23:48:16 > top of Java-index,Core,Core APIs...
# 5
Hey ,Thanks georgemc .You have given valueable time for me, thank u very much.The problem is solved by JarClassLoader.Thanks-bunty.
bunty_bargea at 2007-7-8 23:48:16 > top of Java-index,Core,Core APIs...