The moose likes Other JSE/JEE APIs and the fly likes To Retrive classes from . java file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "To Retrive classes from . java file" Watch "To Retrive classes from . java file" New topic

To Retrive classes from . java file

Nirav Desai

Joined: Mar 06, 2007
Posts: 2
hi ppl.....
i hav got a query.... of how to retrive which all classes are present in . java file... i.e i want the name of the the classes in a . java file...........

actually my original problem is that at runtime i want to find out the class that contain the main() in a . java file...
hope it wil b solved
thanks in advance.....
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Sounds like you need to parse the file with some sort of regular expression to match the pattern you are after.* and java.util.regex.* should help.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
This is tricky, as there is no one particular string you can look for. Even using regexps there are quite few possibilities of how a main method might look like. It would be easier to do this with class files instead of source files, since you can use reflection to look for the one signature that indicates a main method.
I agree. Here's the link:
subject: To Retrive classes from . java file
It's not a secret anymore!