1

I'm currently having a maven project with multiple dependencies (which are included in the jar) and one single dependency which lays somewhere else also compiled as a jar file. Now I'm trying to load classes dynamically in my project, a simple class like this would work:

public class test {
    static {
        System.out.println("SUCCESS!");
    }
}

but as soon as my class is trying to inteact with my project it says that the external dependency could not be resolved. Is there a way to automatically include all dependencies of the project into my classpath when I'm compiling? (I'm already adding the project.jar) Or is there an other way to compile it and execute this class without this problem?

EDIT: It's now compiling because I added the jar manually to my classpath, but I'm getting a runtime exception now

My class:

import be.multicu.core.MultiCube;

public class Debug {

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

The exception (note: the log is reversed!):

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_51]
at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_51]
at be.multicu.core.util.Debugger.debugFromPastebin(Debugger.java:54) [MultiCubeCore.jar:?]
at be.multicu.core.command.commands.debug.run(debug.java:68) [MultiCubeCore.jar:?]
at be.multicu.core.command.BaseCommand.onCommand(BaseCommand.java:55) [MultiCubeCore.jar:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:724) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:985) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:830) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:65) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:176) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.ServerConnection.c(ServerConnection.java:77) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:713) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:283) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:576) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:482) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftcubbit.jar:git-Spigot-1.7.2-R0.3-178-g45eab08]
Caused by: java.lang.NoClassDefFoundError: be/multicu/core/MultiCube
at Debug.debug(Debug.java:6) ~[?:?]
... 21 more
Caused by: java.lang.ClassNotFoundException: be.multicu.core.MultiCube
at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[?:1.7.0_51]
at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[?:1.7.0_51]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_51]
at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[?:1.7.0_51]
at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[?:1.7.0_51]
at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[?:1.7.0_51]
at Debug.debug(Debug.java:6) ~[?:?]
... 21 more

Thanks for the help, Joba

Joba
  • 777
  • 7
  • 24
  • Are you aware of the Class-Path entry in the manifest? – Thorbjørn Ravn Andersen May 17 '14 at 08:09
  • There is, for some weird reason, no class path entry in the manifest of my project, but I can access the external jar and it's definitly not included in my project. – Joba May 17 '14 at 20:05
  • Edited main post, as the problem changed a bit – Joba May 17 '14 at 21:41
  • This line `at be.multicu.core.util.Debugger.debugFromPastebin(Debugger.java:54) [MultiCubeCore.jar:?] `refers to a reflection call. There is a mismatch between what is expected and what is found. – Thorbjørn Ravn Andersen May 17 '14 at 23:42
  • o.getClass().getDeclaredMethod("debug").invoke(o); should work, shouldn't it? I'm a bit more concerned about `Caused by: java.lang.NoClassDefFoundError: be/multicu/core/MultiCube at Debug.debug(Debug.java:6) ~[?:?] ... 21 more` – Joba May 18 '14 at 18:45
  • Well, is that class present in the jar? – Thorbjørn Ravn Andersen May 18 '14 at 19:06
  • I'm calling it from a jar which contains this class, and I'm adding this jar to my classpath. – Joba May 19 '14 at 18:36
  • Any other ideas... I have no idea what could cause it anymore... – Joba May 21 '14 at 12:50
  • Merge all your dependencies together in a über jar and deploy that. See http://stackoverflow.com/q/11947037/53897 – Thorbjørn Ravn Andersen May 21 '14 at 13:25
  • This won't help, because the extern dependency is loading, just the one which actually compiles doesn't seem to be able to add itself to the classpath. e.g. /home/cubepanel/test-network/jars/craftcubbit.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeCore.jar craftcubbit is the extern one, it's working. MultiCubeCore is the one which executes my dynamically loaded code, and this one is not working. Is this because I can't add the jar which is compiling to the classpath? – Joba May 22 '14 at 09:24
  • I frankly have no idea. I suggest you write up your exact scenario and deployment environment and add it to your question - currently it has too little information for anyone to do anything but guess. – Thorbjørn Ravn Andersen May 22 '14 at 09:35
  • I would have already done that, but I'm legally not allowed to post the source code. I'll set up a test environment with some dummy code for you. – Joba May 22 '14 at 11:30
  • I do not think I can help you. I would suggest creating a reproducible scenario, and opening a new question (and close this one). – Thorbjørn Ravn Andersen May 22 '14 at 20:23
  • Thats probably the best solution.. – Joba May 24 '14 at 08:07

0 Answers0