Find All public static main methods

A

aidy

Is there a way to find all the public static main methods in a given
set of classes/archives?

Aidy
 
Z

zero

Is there a way to find all the public static main methods in a given
set of classes/archives?

Aidy

What, you just want to find the main methods in a bunch of source code
files? Do a text search.

If you want to find them in compiled classes, try any one of a thousand hex
editors.
 
S

Shorty

And probably other java IDE too.
In eclipse, I guess you'll be looking into "java search"...
 
B

Benji

zero said:
If you want to find them in compiled classes, try any one of a thousand hex
editors.

Did he mean programmatically? If he did, this wouldn't help.

try Class.getMethod().

e.g...

Test.class.getMethod("main", new Class[]{String.class});

so, if you had an array of Class objects, you could do

for(Class c : classes)
if(c.getMethod("main", new Class[]{String.class}) != null)
System.out.println(c);

of course, you'd also have to check to see if it's static...but that's the
general idea.

If you have a list of class *names*, you can convert a class name into a
Class object by invoking Class.forName() on the string.
 
O

Oliver Wong

Shorty said:
And probably other java IDE too.
In eclipse, I guess you'll be looking into "java search"...

Actually, I was thinking of that feature such that when you click on a
project, and choose "run", it lists all the classes within that project
which contain a main method declaration (including in the JAR dependencies),
so that you may select which class it is whose main function you actually
want to invoke.

Useful when you're suddenly assigned to work on a chunk of code, and
you'd like to know what the entry point(s) to the program is/are.

- Oliver
 
S

Shorty

If programmaticcaly is what you need, you'll also need to change the
second parameter, since main takes in a String[] not a String. I don't
quite remember the exact syntax to use arrays that way and don't have
the means to try here, so you'll have to give more hints.

However, be aware that you can only do this on classes that you can
actually load in a JVM, meaning they are in the classpath or you have a
classloader that can load them. you can't just use that directly on any
class in your filesystem.
 
B

Benji

Shorty said:
If programmaticcaly is what you need, you'll also need to change the
second parameter, since main takes in a String[] not a String. I don't
quite remember the exact syntax to use arrays that way and don't have
the means to try here, so you'll have to give more hints.

good point, thanks.

String[].class, iirc.
 
C

Chris Uppal

Shorty said:
However, be aware that you can only do this on classes that you can
actually load in a JVM, meaning they are in the classpath or you have a
classloader that can load them. you can't just use that directly on any
class in your filesystem.

If you are looking at compiled classes, then it'd be better, for several
reasons, to use a byte-code library such as BCEL or ASM for the scanning.

Reasons:

+) Probably faster (I haven't checked but it /ought/ to be).

+) Safer (no risk of class initialisation code running).

+) More flexible (as you say, no need for the .class file to be on the
classpath).

Downside:

-) You have to learn (a bit about) BCEL or ASM[*].

-) ?

-- chris

([*] a couple of people in this group have recently stated their preference for
ASM over BCEL; on the whole I think they're probably right -- not that I've
played much with either package -- but I suspect that the BCEL API is more
straightforward for the occasional user than ASM's Visitor pattern based API)
 
R

Ross Bamford

If you are looking at compiled classes, then it'd be better, for several
reasons, to use a byte-code library such as BCEL or ASM for the scanning.

Hmm... Looks like a good opportunity for a bit of...

<(free) product placement>
Absolutely. He could use Jen (http://jen.dev.java.net) which wraps around
ASM, making this as easy as:

SoftClass sc = new SoftClass(new ClassReader(classFileBytes));
SoftMethod sm = sc.getSoftMethod("void main(String[])");

if (sm != null && (sm.getModifiers() & SoftClass.ACC_STATIC &
SoftClass.ACC_PUBLIC) != 0) {

/* do something, this class has a main method */

}

(for example).
Reasons:

+) Probably faster (I haven't checked but it /ought/ to be).

Appreciably so, since there's no need to load, resolve, and define the
class.
+) Safer (no risk of class initialisation code running).

Most definitely. Any exception (or Error) in a class initializer could
scupper the search, and theres no telling how long they might take, or
what else they might do...
+) More flexible (as you say, no need for the .class file to be on the
classpath).

Downside:

-) You have to learn (a bit about) BCEL or ASM[*].

;) See above.
-) ?

-- chris

([*] a couple of people in this group have recently stated their
preference for
ASM over BCEL; on the whole I think they're probably right -- not that
I've
played much with either package -- but I suspect that the BCEL API is
more
straightforward for the occasional user than ASM's Visitor pattern based
API)

Well, 'more straightforward', perhaps, perhaps not, but either way, Jen
redresses the balance. ASM's performance is blindingly fast compared to
BCEL (around about 5 times as fast IIRC), and Jen takes full advantage of
that, too (hardly impacting performance at all), so it's the best of both
worlds.

</product placement>
 
C

Chris Uppal

Ross said:
<(free) product placement>
[...]

<grin>

My apologies, I had forgotten all about Jen (odd since I read about it only a
couple of weeks ago). In mitigation I offer the excuse that I hadn't realised
that Jen was supposed to ease reading classfiles as well as writing them.

-- chris
 
R

Ross Bamford

Ross said:
<(free) product placement>
[...]

<grin>

My apologies, I had forgotten all about Jen (odd since I read about it
only a
couple of weeks ago). In mitigation I offer the excuse that I hadn't
realised
that Jen was supposed to ease reading classfiles as well as writing them.

-- chris

:) No probs. Writing is probably what most people would use it for, but
it's actually small and fast enough to be useful for pretty much any kind
of processing of .class files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top