I'm trying to load classes from a jar file, some classes are loaded but then it throws a classNotFoundException on some of them (even if I catch the exception, it still throws it I really don't know how).
Here is the code (which I found here and works perfectly for everyone) :
for (File jarFilePath : jarFilePaths) {
pathToJar = jarFilePath.getAbsolutePath();
jarFile = new JarFile(pathToJar);
jarFiles.add(jarFile);
Enumeration e = jarFile.entries();
URL[] urls = { new URL("jar:file:" + pathToJar + "!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);
log.debug("Loading classes from jar : " + jarFile.getName());
while (e.hasMoreElements()) {
JarEntry je = (JarEntry) 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;
try {
c = cl.loadClass(className);
classes.add(c);
log.debug("Loaded : " + className);
} catch (ClassNotFoundException e1) {
log.debug("Did not load : " + className + " - ClassNotFoundException");
//e1.printStackTrace();
}
}
}
This code let me load the classes from the jar, it works well, BUT it throws a ClassNotFoundException on some classes and it isn't catched (my debug doesn't appear and the stack track is printed even if I put it in comment)
Edit : I added a catch for NoClassDefFoundError and now it catches the exception. However, I still don't know why it doesn't load those classes. I checked them, they are full .class files with no $ sign.