0

I am programming a plugin system for my Java Application. My problem is if I tryto cast from the Child Class to my Parent Interface I get the ClassCastException java.lang.ClassCastException: net.scrumplex.testplugin.Main cannot be cast to net.scrumplex.sprummlbot.plugins.SprummlPlugin

My Interface:

package net.scrumplex.sprummlbot.plugins;

import com.github.theholywaffle.teamspeak3.api.event.BaseEvent;
import com.github.theholywaffle.teamspeak3.api.event.TS3EventType;

public interface SprummlPlugin {

    public boolean init(String version);

    public void end();

    public void handleEvent(TS3EventType type, BaseEvent event);

}

My TestPlugin:

package net.scrumplex.testplugin;

import com.github.theholywaffle.teamspeak3.api.event.BaseEvent;
import com.github.theholywaffle.teamspeak3.api.event.TS3EventType;

import net.scrumplex.sprummlbot.plugins.SprummlPlugin;

public class Main implements SprummlPlugin {

    @Override
    public void end() {
        System.out.println("Stopped");
    }

    @Override
    public void handleEvent(TS3EventType type, BaseEvent event) {
        System.out.println(type.name());
    }

    @Override
    public boolean init(String arg0) {
        System.out.println("Test");
        return true;
    }

}

My PluginLoader:

public static void load(File jarFile) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
        IOException, PluginLoadException {
    String path = "";
    JarFile jar = new JarFile(jarFile);
    JarEntry entry = jar.getJarEntry("plugin.ini");
    InputStream input = jar.getInputStream(entry);
    Ini ini = new Ini(input);
    if (!ini.containsKey("Plugin")) {
        jar.close();
        throw new PluginLoadException("Ini file not compatible.");
    }
    Section sec = ini.get("Plugin");
    if (sec.containsKey("main")) {
        path = sec.get("main");
    }
    ClassLoader loader = URLClassLoader.newInstance(new URL[] { jarFile.toURI().toURL() });
    SprummlPlugin plugin = (SprummlPlugin) loader.loadClass(path).newInstance();
    pluginslist.add(plugin);
    plugin.init(Vars.VERSION);
    jar.close();
}

The class path for the TestPlugin is correct

Scrumplex
  • 522
  • 1
  • 6
  • 15

1 Answers1

0

How I fixed this:

I added this.getClass().getClassLoader()to my URLClassLoader.newInstance(...);

Scrumplex
  • 522
  • 1
  • 6
  • 15