被调用的java类/方法能否访问主类路径?

被调用的java类/方法能否访问主类路径?,java,reflection,invoke,Java,Reflection,Invoke,经过一些研究,我成功地从jar文件中的类调用了一个方法,如下所示: URLClassLoader child = new URLClassLoader (new URL[] {(new File("myJar.jar")).toURL()}, Main.class.getClassLoader()); Class<?> classToLoad = Class.forName ("com.myClass.TEST", true, child); Meth

经过一些研究,我成功地从jar文件中的类调用了一个方法,如下所示:

URLClassLoader child = new URLClassLoader (new URL[] {(new File("myJar.jar")).toURL()}, Main.class.getClassLoader());
        Class<?> classToLoad = Class.forName ("com.myClass.TEST", true, child);
        Method method = classToLoad.getDeclaredMethod ("onEnable");
        Object instance = classToLoad.newInstance ();
        Object result = method.invoke (instance);
其中TestString只是“helloworld”,TempClass位于“main”jar中, 测试类在myJar.jar中

在调用onEnable()方法时,它为TempClass抛出一个NoClassDefError

“客户端”jar是否有办法在运行时访问“主”jar的类路径


我们的目标是调用“插件”,就像Craftbukkit/Spigot如何与插件一起工作一样,供那些使用过插件的人使用。

Oops!我想点击Java。现在修好了,哎呀!我想点击Java。现在修好了。
public void onEnable() {
    System.out.println(TempClass.TestString);
}