I got the following classes in a spring project
interface IStep {
IStep doStep();
String getName();
}
@Component
public class Step1 implements IStep {
public final static String NAME = "STEP1";
...
}
@Component
public class Step1 implements IStep {
...
}
A jar is built from this project.
Using a plain simple java project, i'm trying to load the jar to analyse the dependencies between classes and create some diagrams. My first use case is to find all the classes that implement IStep and get their name.
I'm trying to use reflection to load the jar, get the classes and load them. However, I keep getting a noclassdeffounderror on Step1. How can I load the class?
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") || !je.getName().contains("mypackagename")){
continue;
}
// -6 because of .class
String className = je.getName().substring(0,je.getName().length()-6);
className = className.replace('/', '.');
Class c = cl.loadClass(className); //noclassdeffounderror
}
The spring boot plugin generates a unique structure by default: See the Spring docs for more details.
You will need
Then you can use the Spring tooling to do something like this