mardi 12 septembre 2017

Can a invoked java class/method access the main classpath?

After some research, I have managed to invoke a method from a class in a jar file like so:

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);

TEST class:

public void onEnable() {
    System.out.println(TempClass.TestString);
}

Where TestString is just "Hello world" and TempClass is in the "main" jar, and TEST class is in myJar.jar

While invoking the onEnable() method, it throws a NoClassDefError for TempClass

Is there a way the "Client" jar can access the "Main" jar's classpath at runtime?

The goal is to invoke "plugins" much like how Craftbukkit/Spigot works with Plugins,for those who have played with that.





Aucun commentaire:

Enregistrer un commentaire