Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Find All public static main methods

Thread view: 
aidy - 02 Nov 2005 21:03 GMT
Is there a way to find all the public static main methods in a given
set of classes/archives?

Aidy
zero - 02 Nov 2005 21:40 GMT
"aidy" <aidy.rutter@gmail.com> wrote in news:1130961806.395448.200130
@g47g2000cwa.googlegroups.com:

> 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.
Benji - 02 Nov 2005 22:53 GMT
> 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.

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

Shorty - 02 Nov 2005 23:12 GMT
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.
Benji - 02 Nov 2005 23:42 GMT
> 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.

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

Chris Uppal - 03 Nov 2005 10:06 GMT
> 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)
Ross Bamford - 03 Nov 2005 15:13 GMT
>> 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
[quoted text clipped - 3 lines]
> 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.

> -) ?
>
[quoted text clipped - 8 lines]
> 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>

Signature

Ross Bamford - rosco@roscopeco.remove.co.uk

Chris Uppal - 03 Nov 2005 17:39 GMT
> <(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
Ross Bamford - 03 Nov 2005 18:34 GMT
>> <(free) product placement>
> [...]
[quoted text clipped - 8 lines]
>
>     -- 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.

Signature

Ross Bamford - rosco@roscopeco.remove.co.uk

Oliver Wong - 02 Nov 2005 22:35 GMT
> Is there a way to find all the public static main methods in a given
> set of classes/archives?

   Yes, Eclipse does it.

   - Oliver
Shorty - 02 Nov 2005 22:42 GMT
And probably other java IDE too.
In eclipse, I guess you'll be looking into "java search"...
Oliver Wong - 02 Nov 2005 23:00 GMT
> 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
Shorty - 02 Nov 2005 23:06 GMT
Didn't think about that one, but you're right. Forget what I said Aidy.
Roedy Green - 03 Nov 2005 03:19 GMT
>Is there a way to find all the public static main methods in a given
>set of classes/archives?

There are three basic approaches:

1. let and IDE do it and tap in that.  e.g. Eclipse.

2. use a byte class library parser and find them in the class
files/jars.  See http://mindprod.com/jgloss/jasm.html

3. use a Regex to scan the source for the patterns you want.
http://mindprod.com/jgloss/regex.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.