I need to read a Spring Boot jar and load all the clases on a ClassLoader.
My problem,in spring boot classes are on "/BOOT-INF/classes" directory and not on the root directory.
Anybody knows how i can load this classes in my ClassLoader?
I try to do this:
private URLClassLoader getURLClassLoaderFromJar(Path jarPath) throws MalformedURLException {
return URLClassLoader
.newInstance(new URL[] { new URL("jar:file:" + jarPath.toAbsolutePath().toString() + "!/") });
}
This load the jar, but no the classes inside /BOOT-INF/classes
I finally opted for decompress de jar on a temporary directory and create a URLClassloader with this entries: One to the root directory. One to the BOOT-INF/classes And one for every jar in BOOT-INT/lib
Path warDirectory = decompressWar(absolutePathFile);
File rootDir = new File(warDirectory.toAbsolutePath().toString());
File springBootDir = new File(warDirectory.toAbsolutePath().toString() + "/BOOT-INF/classes/");
List<URL> listaURL = new ArrayList<URL>();
listaURL.add(rootDir.toURI().toURL());
listaURL.add(springBootDir.toURI().toURL());
//This scan the BOOT-INF/lib folder and return a List<URL> with all the libraries.
listaURL.addAll(getURLfromSpringBootJar(warDirectory));
URL[] urls = new URL[listaURL.size()];
urls = listaURL.toArray(urls);
cl = new URLClassLoader(urls);
//This explore the JAR and load all the .class fulies to get the className.
resultClassesBean = loadJars(Collections.singletonList(pathJarFile), cl);
if(resultClassesBean != null && resultClassesBean.getListResultClasses() != null && !resultClassesBean.getListResultClasses().isEmpty()) {
for(String clazz : resultClassesBean.getListResultClasses()) {
cl.loadClass(clazz);
}
}