ClassNotFoundException when loading a class at runtime

半城伤御伤魂 提交于 2019-12-11 19:53:39

问题


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

来源:https://stackoverflow.com/questions/25327791/classnotfoundexception-when-loading-a-class-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!