Reputation: 21
I'm working on a university java project that requires me to implement a simple plugin architecture. A main application in Project A would have to load plugins from a specified directory located in another project B. After having done some research on the topic (including stackoverflow), I decided to go with URLClassLoader to load the classes which I might then instatiate. Project B references Project A and all plugins extend a common plugin class (might as well be an interface but should not make any difference). This is what I have got so far:
The source for the plugin class I try to load:
package plugin;
import de.uks.student.pluginpattern.model.EditorPlugin;
public class Circle extends EditorPlugin
{
@Override
public void init()
{
}
}
and the code that is supposed to load the class:
public void init(String[] args)
{
editorPane = new EditorPane().withWidth(500).withHeight(500);
toolBar = new ToolBar();
plugins = new EditorPluginSet();
// load plugins
File pluginDir = new File(PLUGIN_PATH);
if (!pluginDir.exists())
{
System.err.println("Plugin path not found!!");
return;
}
String[] plugins = pluginDir.list();
if (plugins == null)
{
System.err.println("Plugin path points to a file!!");
return;
}
for (String pluginString : plugins)
{
for (String argString : args)
{
if (pluginString.contains(argString))
{
System.out.println("Loading plugin from " + pluginString);
File pluginFile = new File(PLUGIN_PATH + "/");
// as getAbsolutePath embeds the relative path ... /../pluginProject ...
// which may not be processed correctly by the OS (Windows at least),
// we correct the path manually (just to be on the safe side)
String absolutePath = pluginFile.getAbsolutePath();
String[] split = absolutePath.split("\\\\");
List<String> splitsimpleList = Arrays.asList(split);
ArrayList<String> splitList = new ArrayList<>(splitsimpleList);
for (int i = 0; i < splitList.size() - 1; ++i)
{
if (splitList.get(i + 1).equals(".."))
{
splitList.remove(i);
splitList.remove(i);
break;
}
}
StringBuilder b = new StringBuilder();
for (String string : splitList)
{
b.append(string);
b.append("/");
}
File pluginFileForRealThisTime = new File(b.toString());
URL pluginURL = null;
try
{
pluginURL = pluginFileForRealThisTime.toURI().toURL();
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
URL[] urls = {pluginURL};
ClassLoader parentClassLoader = this.getClass().getClassLoader();
URLClassLoader uLoader = new URLClassLoader(urls, parentClassLoader);
Class<?> pluginClass = null;
try
{
String className = "plugin." + argString;
pluginClass = uLoader.loadClass(className);
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Now I always end up with a ClassNotFoundException:
java.lang.ClassNotFoundException: plugin.Circle at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at de.uks.student.pluginpattern.model.EditorSystem.init(EditorSystem.java:412) at de.uks.student.pluginpattern.EditorSystem.main(EditorSystem.java:13)
I've already checked that
I've pretty much run out of ideas on what else to try. What am I doing wrong?
Upvotes: 2
Views: 3575
Reputation: 21
I had to point the ClassLoader to the bin folder instead of the bin/plugins folder
Upvotes: 0