How would I fix this classloader issue?
-
05-07-2021 - |
Question
Let me start off by saying I'm not the best at Java so I may have missed something really simple...
I'm working on a plugin system and everything works perfectly fine in eclipse but not when I actually launch the applet outside of it.
I'm trying to load a .class
file from a directory and execute it, I know why it isn't working I just don't know what to do to solve it.
The issue is that Eclipse is referencing the applet itself in the class-path but it's not being referenced in the class-path outside of eclipse and I don't have access to -cp
or -classpath
, I can only launch it via a launcher designed to load the jar with a fixed class-path.
Any ideas on how I would be able to solve this issue?
Loader Code:
final ClassLoader loader = new URLClassLoader(new URL[] { new File("C:/Plugin/Directory/").toURI().toURL() });
final Class<?> c = loader.loadClass("PluginFile");
Object s = c.newInstance();
s.getClass().getMethod("onLoad").invoke(s);
Plugin Code:
import net.plugintest.pluginsystem.*;
public class PluginFile extends ClientPlugin
{
private String Author = "Plugin Author";
private String Description = "Plugin Description"
private Player p = new Player();
@Override
public void onLoad()
{
p.talk("Testing");
}
}
This problem has been driving me up the wall for a week now, I don't know what to do. :/
Solution
I'm not sure but try providing parent classloader to the classloader you create, eg:
ClassLoader parent = Thread.currentThread().getContextClassLoader();
// or
// ClassLoader parent = getClass().getClassLoader();
ClassLoader loader = new URLClassLoader(new URL[] { new File("C:/Plugin/Directory/").toURI().toURL() }, parent);