I want to do a plugin system for a sprite sheet editor. I found a solution with :
protected void loadPlugin() throws MalformedURLException
{
File rep = new File(repPlugin);
URL url = rep.toURI().toURL();
URL[] urls = new URL[]{url};
try
{
this.getClass().getClassLoader().loadClass("net.bonbonchan.tools.spriteanim.plugin.Plugin");
}
catch(Exception ex) { System.err.println("Class not found for Plugin !!! O_o"); }
URLClassLoader loader = new URLClassLoader(urls);
plugins.removeAllElements();
File fic[] = rep.listFiles();
for(int i=0;i<fic.length;i++)
{
if (fic[i].getName().endsWith(".class"))
{
try
{
System.out.println("Loading class : "+fic[i].getAbsolutePath());
String fileName = fic[i].getName();
String className = fileName.substring(0,fileName.indexOf("."));
Class fileClass = loader.loadClass(className);
Object fileObject = fileClass.newInstance();
if (fileObject instanceof Plugin)
{
Plugin plugin = (Plugin)fileObject;
plugins.add(plugin);
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}
pluginListModel.setPlugins(plugins);
}
So plugins are subclass of net.bonbonchan.tools.spriteanim.plugin.Plugin in the default package.
In netbean, it work fine. With the jar, it work fine. With JavaWebStart, it doesn’t work ???
Java Web Start 1.6.0_05
Utilisation de la version JRE 1.6.0_05 Java HotSpot(TM) Client VM
...
Loading class : E:\ ... \classes\JSAPacker$1.class
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: net/bonbonchan/tools/spriteanim/plugin/Plugin
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
...
at java.lang.Class.newInstance(Unknown Source)
at net.bonbonchan.tools.spriteanim.JMain.loadPlugin(JMain.java:236)
...
Caused by: java.lang.ClassNotFoundException: net.bonbonchan.tools.spriteanim.plugin.Plugin
...
The line 236 is : Object fileObject = fileClass.newInstance();
I really don’t get it.
Why it is thrown at line 236 and not at this.getClass().getClassLoader().loadClass(“net.bonbonchan.tools.spriteanim.plugin.Plugin”); ??
Why it is thrown and NOT CATCH by catch(Exception ex) ???
It is the first time that I do a plugin system, so if someone can help me