I have a folder full of classes on which I want to load.
My URLClassLoader
seems to load every other class just fine, but sometimes it misbehaves for some reason and it is driving me nuts. All the classes listed in the folder are basically empty (No constructors, fields, methods) and belong to their own package.
package death;
public class Death{
}
package death;
public class Awesome{
}
package abc.dfg.hijk;
public class Test {
}
package mesa;
public class ZZ{
}
I have a mechanism that extracts the package on which a class is part of, as printed out on the console.
This is the code Im using to load each class
String fileName = file.getName();
if (fileName.contains(".class")) {
URL[] urls = new URL[]{ classesFolder.toURI().toURL() };
URLClassLoader loader = URLClassLoader.newInstance(urls);
String packageName = getPackage(file);
//substring -6 because of .class
Class clazz = loader.loadClass(packageName + fileName.substring(0, fileName.length() - 6));
loader.close();
}
System.out.println("Done loading " + file.getName());
System.out.println("");
I aint exactly sure whats going on here, though I feel like Im definitely missing something, as all the other classes seem to load just fine. The value of packageName
is correct for all the classes that were loaded. Any idea of what Im doing wrong?