aspose file tools*
The moose likes Java in General and the fly likes how to get package and class name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to get package and class name?" Watch "how to get package and class name?" New topic
Author

how to get package and class name?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 388
All of the classloaders (eg java.lang.classloader) require I supply a package and class name in addition to a file name or byte array.

Suppose I don't know the class and package name and want to enumerate all the classes in a .class file. How do I do this?

Thanks,
Siegfried
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There is never more than one class in a class file.

You have to solve the name/package problem for yourself, depending on the situation in your specific class loader implementation. The standard Java application class loader expects that a file named Foo.class holds a class named Foo, and it expects that if the file is located in com/bar/Foo.class relative to a jar file root or to a classpath entry, that the package is com.bar. Your solution may be the same, or it may be different; depends on what you're doing.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Perhaps you've got a normal classloader and you know the path to the file, but you don't know which of the parent directories is the base directory, and which are part of the package structure. You can work out the class name from the file name, but the package is less obvious. One approach would be to work your way up (or down) the directory structure, trying each directory in turn as if it's the base (and the subdirectories, part of the package). Try to load the given class using that info, and if it doesn't work, catch the exception and move on to the next possibility. It's a little inelegant, but may be the simplest way to go about this.

Alternately, the info you need is in the class file, in a format which is not platform- or classloader-dependent. You can parse the class file directly to find out the class and package info. It's possible to do this yourself by studying the class file format a bit. But it's probably a bit tedious, as you need to read in the entire constant table first, consisting of a number of different structures of varying length. Then you can get to the this_class field, which tells you which of the structures in the previous constant table identified the class.

Or, you could use something like BCEL to parse this for you. Glancing through their cleverly-hidden API :roll: , this could be as simple as new ClassParser(fileName).parse().getPackageName(). Or not - as I recall from limited experience (and comments of others), BCEL can have its share of frustrating gotchas. Still, it may well be the easiest solution here if you don't mind adding the third-party jar to your project.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get package and class name?