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 Java in General and the fly likes Easiest Way to get Class information? 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 » Java in General
Bookmark "Easiest Way to get Class information?" Watch "Easiest Way to get Class information?" New topic
Author

Easiest Way to get Class information?

Andy Peter
Greenhorn

Joined: Feb 12, 2002
Posts: 12
I have the absolute path to a Java Class File.
For this Class I need to find information like the methods/package information/ etc.
Which is the cleanest and easiest way to do this. Can some one explain this or point to any examples?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
The java.lang.Class class has methods in it for finding out all that information.
To get the Class object of your class, look at the static Class.forName() method.
It wants the name of a class that can be located on the classpath, however, and not an absolute path name. So, you'll have to make sure that the class file is accessible on the JVM's classpath by setting up it's location before you invoke the JVM.


Rob
SCJP 1.4
Andy Peter
Greenhorn

Joined: Feb 12, 2002
Posts: 12
So I assume I just need to set the classpath to the directory where the classfile is located and I do not need to know the package information right?
suppose i have a class
C:\Test\org\music\Guitar.class
I set the classpath to C:\Test\org\music\
and then a
Class c = Class.forName("Guitar") will give me a
Guitar Class
and c.getPackage().getName() will give me org.music ? if org.music is the package name?
-- Thanks.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Andy, that looks correct to me.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Easiest Way to get Class information?
 
Similar Threads
Maven artifact notification
Getting Class information
How to be a moderator, sheriff ?
Notify swing gui of an event
security password