how to get package name from a class file?

1.2k views Asked by At

I want to get package name from a .class file, but after trying to use org.apache.bcel, I am not satisfied with it because there are always some classes which can not be parsed.

the rate is about 20 over 500.

So, is there any other library which can get the package name from a class file?

Edit: this is how I get a package name from a class file now. This can achieve my goal, but some class files can not be parsed, so I want to know is there a better way.

import org.apache.bcel.classfile.ClassParser;

        String filePath = "/tmp/Test.class"
        ClassParser classParser = new ClassParser(filePath);

        JavaClass javaClass;
        try {
            javaClass = classParser.parse();
        } catch (IOException | ClassFormatException e) {
            System.out.println(filePath);
        }

        String packageName = javaClass.getPackageName();
0

There are 0 answers