I'm trying to make a classloader/modloader for my program. So far I have this loader:
// methodTB - swing text field for method
// classTB - swing text field for class
// classCB - swing check box for default class (which is the file name)
// Grab the URL of the file
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
// Make a ClassLoader
ClassLoader cl = new URLClassLoader(urls);
// If the class name = file name
if (classCB.isSelected()) {
// Get the name of the class
className = file.getName();
// Remove the file extension
className = className.substring(0, className.lastIndexOf('.'));
}
// Else
else {
// Grab directly from the TB
className = classTB.getText();
}
// Load it
Class cls = cl.loadClass(className);
Method method = cls.getDeclaredMethod(methodTB.getText());
Object instance = cls.newInstance();
Object result = method.invoke(instance);
This works for classes that already exist within the program, however, when trying to load other classes, the following exception pops up:
java.lang.ClassNotFoundException: example
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at modloader.lambda$menu$1(modloader.java:116) <4 internal calls>
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) <31 internal calls>
Is there any way I can load classes that are outside of the program?
(For context, here's the full code)