ClassNotFoundException when loading a class at run

2019-09-06 15:46发布

问题:

Using the Bukkit-API 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 one of the already loaded plugins. (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:

/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

import be.multicu.core.MultiCube;

public class Test{

    public void debug() {
        System.out.println(MultiCube.getInstance());
    }
}

Working

import org.bukkit.Bukkit;

public class Test{

    public void debug() {
        System.out.println(Bukkit.getClass().getName());
    }
}

Sourcecode of RuntimeCompiler.java: http://paste.multicu.be/axigofekip.avrasm (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 classpath of MultiCube.jar which means that the classpath must be correct since the sourcecode can be compiled.

EDIT: I was able to fix it by adding MultiCube.class.getClassLoader() as an argument in the constructor of my URLClassLoader