How to load classes at runtime from a folder or JAR?

I am trying to create a Java tool that scans the structure of a Java application and provides some meaningful information. To do this, I need to be able to scan all .class files from the project location (JAR / WAR or just the folder) and use reflection to read my methods. This is almost impossible.

I can find many solutions based on URLClassloader that allow loading specific classes from a directory / archive, but none of them will allow me to load classes without any information about the class name or package structure.

EDIT: I think I phrased it poorly. My problem is not that I cannot get all the class files, I can do it with recursion, etc. And find them right. My problem is getting a Class object for each class file.

+49
java reflection jar class load
Jun 13 2018-12-12T00:
source share
3 answers

The following code loads all classes from a JAR file. He does not need to know anything about classes. Class names are retrieved from JarEntry.

JarFile jarFile = new JarFile(pathToJar); Enumeration<JarEntry> e = jarFile.entries(); URL[] urls = { new URL("jar:file:" + pathToJar+"!/") }; URLClassLoader cl = URLClassLoader.newInstance(urls); while (e.hasMoreElements()) { JarEntry je = e.nextElement(); if(je.isDirectory() || !je.getName().endsWith(".class")){ continue; } // -6 because of .class String className = je.getName().substring(0,je.getName().length()-6); className = className.replace('/', '.'); Class c = cl.loadClass(className); } 

edit:

As suggested in the comments above, the javassist will also be an opportunity. Initialize ClassPool somewhere before the while loop, generating the code above, and instead of loading the class with the class loader, you can create a CtClass object:

 ClassPool cp = ClassPool.getDefault(); ... CtClass ctClass = cp.get(className); 

From ctClass you can get all methods, fields, nested classes, .... Take a look at javassist api: https://jboss-javassist.imtqy.com/javassist/html/index.html

+86
Jun 13 2018-12-12T00:
source share

List of all classes inside the jar file.

 public static List getClasseNames(String jarName) { ArrayList classes = new ArrayList(); if (debug) System.out.println("Jar " + jarName ); try { JarInputStream jarFile = new JarInputStream(new FileInputStream( jarName)); JarEntry jarEntry; while (true) { jarEntry = jarFile.getNextJarEntry(); if (jarEntry == null) { break; } if (jarEntry.getName().endsWith(".class")) { if (debug) System.out.println("Found " + jarEntry.getName().replaceAll("/", "\\.")); classes.add(jarEntry.getName().replaceAll("/", "\\.")); } } } catch (Exception e) { e.printStackTrace(); } return classes; } 
+8
Jun 13 2018-12-12T00:
source share

To do this, I need to be able to scan all .class files from the project location (JAR / WAR or just a folder)

Scanning all the files in a folder is simple. One option is to call File.listFiles() on a File , which denotes a folder, and then iterate through the resulting array. To navigate subfolder trees, use recursion.

You can scan JAR files using the JarFile API ... and you don’t need to recursively go through the subfolders.

None of them are particularly complex. Just read javadoc and start coding.

+6
Jun 13 2018-12-12T00:
source share



All Articles