• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get package and class name?

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

Thanks,
Siegfried
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic