0

I am currently experiencing another strange problem with Java.

My goal was to make a small plugin system to provide an API for one of my applications. I managed to do such a thing and it works pretty good in Windows. But when I try to run the same application with the same plugin in Linux (Debian 7), it throws a ClassCastException.

Here's the loader:

public ArrayList<ServerPlugin> loadPlugins() throws ServerException
{
    ArrayList<ServerPlugin> plugins = new ArrayList<ServerPlugin>();

    File pluginDir = new File("plugins");

    IMServer.getServer().getSystemLogger().log(Logger.INFO, "Plugin Folder: " + pluginDir.getAbsolutePath());

    File[] jars = pluginDir.listFiles(new FileFilter()
    {
        public boolean accept(File pathname)
        {
            return pathname.getName().endsWith(".jar");
        }
    });

    try
    {
        for (File f : jars)
        {
            URLClassLoader loader = URLClassLoader.newInstance(new URL[] { f.toURI().toURL() });
            ResourceBundle props = ResourceBundle.getBundle("plugin", Locale.getDefault(), loader);
            final String pluginClassName = props.getString("MainClass");
            final String pluginName = props.getString("PluginName");
            IMServer.getServer().getSystemLogger().log(Logger.INFO, "Enabling " + pluginName);

            Object pluginObject = loader.loadClass(pluginClassName).newInstance();
            if (pluginObject instanceof ServerPlugin)
            {
                ServerPlugin plugin = (ServerPlugin) pluginObject;

                plugin.onLoad();

                plugins.add(plugin);
            }

        }
    }
    catch (Exception ex)
    {
        IMServer.getServer().getSystemLogger().log(Logger.ERROR, "Unknown error in plugin system: \n");
        ex.printStackTrace();
    }

    return plugins;
}

As you can see, I tried to prevent the exception by building in an instanceof check. Now I don't get the exception but also the plugin is not loading (kinda obvious).

And here is the code of the plugin:

public class Plugin implements ServerPlugin {

    @Override
    public CommandList getCommandList() {
        CommandList commands = new CommandList();
        commands.add("SEND");

        return commands;
    }

    @Override
    public void onCommand(String command, String[] args, User sender) throws ServerException {

        if (args[1].equalsIgnoreCase("!test"))
        {
            String message = "Server:Testplugin";
            message += ";" + timestamp();

            try
            {
                sender.getOutputStream().write(message + "\n");
                sender.getOutputStream().flush();
            }
            catch (IOException ex)
            {
                throw new CommunicationException(ex.getMessage());
            }
        }

    }

    private String timestamp()
    {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd/HH-mm-ss");
        Date currentTime = new Date(System.currentTimeMillis());
        return format.format(currentTime);
    }

    @Override
    public void onLoad() {
        System.out.println("Hello from test plugin!");
    }

    @Override
    public void onUnload() {
        System.out.println("Goodbye from test plugin :-(");
    }

}

Of course, I have a package declaration and imports, I just cut them off to make it more readable.

Now, this is the plugin.properties file, used to load the plugin:

MainClass=me.test.Plugin
PluginName=Testplugin

The line which was throwing the exception before I built in the instanceof thingy is ServerPlugin plugin = (ServerPlugin) pluginObject; and the stack trace is as follows:

java.lang.ClassCastException: me.test.Plugin cannot be cast to net.dreamcode.im.server.plugins.ServerPlugin
        at net.dreamcode.im.server.plugins.PluginManager.loadPlugins(PluginManager.java:68)
        at net.dreamcode.im.server.IMServer.startServer(IMServer.java:165)
        at net.dreamcode.im.server.IMServer.main(IMServer.java:70)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

I am wondering why there is a call to a part of Eclipse. Maybe this is the cause of my problem?

Thanks for help in advance!

Niklas S.
  • 1,046
  • 10
  • 22

0 Answers0