searching a .class file-Please Help

Hi - I'm new to working in java, and I don't know exactly where this should be posted, I've spent the last week trying to figure this out. So I have the .class or .jar files of a program, what I want to be able to do is figure out which ones contain the static void main method, so that I know which files are runnable, in that same vain I also ant to be able to pull out the package name.

I've already tried to just do it by looking at the .java source files, but that didn't leave much room in terms of error checking. IE the programer accidently added a main method to a .java file and didn't recompile. or its incased in comments. or the source files aren't on the $CLASSPATH.

Please, any ideas, places to look, etc. would be very helpful.

Oh, I"ve been reading the specs for the VM, cause that seemed like a plossible way to go about searching the .class files, but I don't quite fully understand how to impliment searches of the information im looking for using the data in the VM specs.

Thanks again in advanced...

-Hux

[1079 byte] By [Fenir] at [2007-9-26 2:02:51]
# 1
You can do this using Java Reflection APICheck this out1. http://java.sun.com/j2se/1.3/docs/api/java/lang/reflect/package-summary.html2. http://developer.java.sun.com/developer/JDCTechTips/2001/tt0712.html#tip2
pbalaram at 2007-6-29 8:44:52 > top of Java-index,Archived Forums,Java Programming...
# 2

Not sure which of the following you want.

Using classloader, you can load any class file into JVM.

Using Class object, you can find out the package of a Java object.

Using java reflection, you can find out all the methods signatures of a java class.

I don't quite understand what you mean by "go about searching the .class files". Can you give an example?

--lichu

lichudang at 2007-6-29 8:44:52 > top of Java-index,Archived Forums,Java Programming...
# 3

Basically what I want to be able to do is to basically create a file filter that will display a list on programs that are executable in the current directory or among the classpath directories. So I think I need to have to be able to search a .class file to find out if it is an exicutable file. Whatever I have do, has to be dynamic, cause it changes druing runtime.

thanks again

-hux

Fenir at 2007-6-29 8:44:52 > top of Java-index,Archived Forums,Java Programming...
# 4

Then you have to use all three of the techniques:

First, use classloader to load the class files.

Second, use the loaded Class object to find out the full package of that class. (cause you are going to provide a run statement, right?)

Third, use java reflection to check if this class has a public static main method there.

It sure will be a bit complicated. However, if you break it down to reuseable components, that would be very beneficial.

--lichu

lichudang at 2007-6-29 8:44:52 > top of Java-index,Archived Forums,Java Programming...