Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the package name given a class name?

Given a class name as a string, how do I get the package name of it at run time ? I do not have the fully qualified name with package name + class name. Simply only the class name.

I want the package name to be used in Class.forName() method.

I am perfectly fine with finding the first matching package name (if multiple packages have the same class).

Any ideas?

UPDATE

I DO NOT have a Class instance to work on. My requirement is to create a Class using the Class.forName() method. But I simply have ONLY the class name as a string. I need some way to loop though the packages and identify if the class I have belongs to the package.

The stack trace of the exception is

Exception in thread "main" java.lang.ClassNotFoundException: MyAddressBookPage
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
like image 671
Ranhiru Jude Cooray Avatar asked Jan 05 '12 13:01

Ranhiru Jude Cooray


2 Answers

This is most likely an incredibly inefficient, bloated, inconvenient way of doing what you're trying to achieve, and hopefully there's already an out-of-the-box, single way to do it... but it should work.

Basically, scan through every class in the class path, until you find a class where getSimpleName() matches the class name you have.

I recommend looking at Google Classpath Explorer to help manage the nuts and bolts of doing this.

It could look something like this:

 ClassPath classpath = new ClassPathFactory().createFromJVM();
 RegExpResourceFilter regExpResourceFilter = new RegExpResourceFilter(".*", ".*\\.class");
 String[] resources = classpath.findResources("", regExpResourceFilter);

resources is an array of Strings like 'com/foo/bar/Baz.class'. You can now simply loop through and find matching entries, and transform them from slashed to dotted, strip out '.class', etc. Just be careful around trying to match inner classes, as they will have a '$' character in them.

Also, as far as I am aware, this will NOT cause those classes to be loaded.

like image 87
Grundlefleck Avatar answered Oct 06 '22 00:10

Grundlefleck


 final Package[] packages = Package.getPackages();
    final String className = "ArrayList";

    for (final Package p : packages) {
        final String pack = p.getName();
        final String tentative = pack + "." + className;
        try {
            Class.forName(tentative);
        } catch (final ClassNotFoundException e) {
            continue;
        }
        System.out.println(pack);
        break;
    }
like image 22
Fortunato Avatar answered Oct 05 '22 22:10

Fortunato