• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

how to get package and class name?  RSS feed

Ranch Hand
Posts: 420
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

author and iconoclast
Posts: 24203
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
how do I do my own kindle-like thing - without amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!