1

How to get java.lang.Class from *.class file?

I have a folder whicj contains *.class files. I need to reflect them all to java.lang.Class. I've read this answer and did so, as stated in the first option:

        URL[] classUrl = new URL[1];
        classUrl[0] = new URL("file:" + classFilesFolder);
        URLClassLoader ucl = new URLClassLoader(classUrl);
        Class c = ucl.loadClass("I DON'T KNOW CLASS NAMES HERE!");

The problem is that I don't know names of classes that I'm reflecting. How can I solve this problem?

4

2 回答 2

0

If classFilesFolder is /home/user and the files inside the folder have names like /home/user/com/something/My.class, the class name (including the package) will be com.something.My. So the class name is the full file name, with classFilesFolder removed, then change all forward slashes to dots and remove .class at the end.

于 2012-08-17T13:25:05.047 回答
0

You can use asm to parse a class file, and then call getClassName() to find the name.

于 2012-08-17T13:48:56.633 回答