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?
If
classFilesFolderis/home/userand the files inside the folder have names like/home/user/com/something/My.class, the class name (including the package) will becom.something.My. So the class name is the full file name, withclassFilesFolderremoved, then change all forward slashes to dots and remove.classat the end.