Solved Exception when loading class files at runtime

Discussion in 'Plugin Development' started by Jobi, Aug 17, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Jobi

    I'm currently trying to create a plugin which can compile java code of a given pastebin link at runtime and execute that code. Everything is working so far except for one thing: I'm getting a ClassNotFoundException when I'm trying to access the main class of the plugin which loaded the class at runtime. (I'm not getting an exception when I'm using Bukkit-API methods!)
    All plugin jars have been added to the classpath; it currently looks like this:


    Code:
    /home/cubepanel/test-network/jars/craftcubbit.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeHub.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeCore.jar:

    The classes I tried to load dynamically:

    ClassNotFoundException for MutliCube

    Code:
    import be.multicu.core.MultiCube;
     
        public class Test{
            public void debug() {
                System.out.println(MultiCube.getInstance());
            }
        }

    Working

    Code:
    import org.bukkit.Bukkit;
        public class Test{
            public void debug() {
                System.out.println(Bukkit.getClass().getName());
            }
        }
    Sourcecode of RuntimeCompiler.java: http://paste.multicu.be/vihikoreco.java (It's a bit long, thats why I used a pastebin link)

    I also noticed that I'm getting an compilation error when I removed the MultiCubeCore.jar of the class' classpath which means that the classpath must be correct since the sourcecode can be compiled when the classpath is set to the MultiCubeCore.jar.
     
  2. When the server starts, does it say anything about not loading multicubecore
     
  3. Offline

    Double0negative

    Can you show an example of something you are trying to load?
     
  4. Offline

    Jobi

    It is loading MultiCubeCore correctly as MultiCubeCore is the plugin which is trying to load the class.
    [​IMG]
     
  5. Offline

    fireblast709

    Jobi your class is loaded in a ClassLoader constructed by Bukkit. In my own plugin (which is able to load and unload modules at runtime), I use the URLClassLoader(URL[] urls, ClassLoader parent) constructor. You should be able to get the parent through getClass().getClassLoader() in your main class (untested)
     
    Jobi likes this.
  6. Offline

    Jobi

    Changed the class loader to this:
    ClassLoader cl = MultiCube.class.getClassLoader();
    Getting a class not found exception for the class I wanted to load.

    EDIT: I was too tired when I tried out your solution, it worked and I'm very thankful as you are the first one who managed to solve this... I already posted this question on stackoverflow twice and once in the spigot forum.
     
Thread Status:
Not open for further replies.

Share This Page