Tuesday, 3 September 2013

Load class within Jar file and get resource

Load class within Jar file and get resource

I know there are already allot similar questions here, but i couldn't get
any smarter form them. I want to load a class inside a jar file, at this
point this is no problem, but when i want to pass the path to my own
ClassLoader it throws an exception it cannot find the class. Is it
possible to load a class inside a jar using an absolute path? For
instance,
Class cls = loader.loadClass(/path/MyPlugin.jar/MyPlugin.class);
But when i do this:
File test = new File("path/plugins/MyPlugin.jar/MyPlugin.class");
System.out.println(test.exists());
It prints out false. I tried using MyPlugin.jar!/MyPlugin.class or
MyPlugin.jar.jar!MyPlugin.class which i've seen sometimes on the web, even
though i don't really know what it means...

When i do this it finds the class:
URLClassLoader loader = new URLClassLoader(new URL[] {
"path/plugins/MyPlugin.jar" });
Class cl = loader.loadClass("MyPlugin");
But now, how can i receive the path? Something like URL url =
cls.getResource("MyPlugin"); (which gives back a null)

No comments:

Post a Comment