This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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

Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 39576
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.

Ping & DNS - updated with new look and Ping home screen widget
I agree. Here's the link:
subject: To Retrive classes from . java file
Similar Threads
To Retrive classes from . java file
how to retrive more than one match
how to get the information from XML file using xml parser in JSP front end
Vector not accesible in JSP
Store Java object in MySQL Database