• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Easiest Way to get Class information?

 
Andy Peter
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy Peter
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy, that looks correct to me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic