AlessioDP / libby

A runtime dependency management library for plugins running in Java-based Minecraft server platforms.
MIT License
76 stars 20 forks source link

NoClassDefFoundError with relocation #40

Closed CMarcoo closed 5 months ago

CMarcoo commented 5 months ago

Facing a NoClassDefFoundError Using libby 2.0.0, Java 17 and Purpur 1.20.4 server.

I use dependency HikariCP, version 5.0.1. My plugin applies relocation using the maven shade plugin, and I also apply obfuscation using the yGuard plugin.

I call this code onEnable(), right before anything else.

java code ```java private void loadLibraries() { if (this.hikariCP != null || this.bcpkix != null || this.bukkitLibraryManager != null) { return; } this.bukkitLibraryManager = new BukkitLibraryManager(this); this.bukkitLibraryManager.addMavenCentral(); this.hikariCP = Library.builder() .groupId("com{}zaxxer") .artifactId("HikariCP") .version("5.0.1") .relocate("com{}zaxxer{}hikari","top{}cmarco{}lightlogin{}libs{}com{}zaxxer{}hikari") .loaderId("hikari-id") .isolatedLoad(true).build(); this.bcpkix = Library.builder() .groupId("org{}bouncycastle") .artifactId("bcpkix-jdk18on") .version("1.77") .relocate("org{}bouncycastle", "top{}cmarco{}lightlogin{}libs{}org{}bouncycastle") .loaderId("bcpkix-id") .isolatedLoad(true).build(); this.bukkitLibraryManager.loadLibraries(this.bcpkix, this.hikariCP); } ```

Then I make relocations and remove any obfuscation from libby in my pom.xml

```xml com.alessiodp.libby top.cmarco.lightlogin.libs.com.alessiodp.libby com.zaxxer.hikari top.cmarco.lightlogin.libs.com.zaxxer.hikari org.bouncycastle top.cmarco.lightlogin.libs.org.bouncycastle ```

Even checked if packages are the same within the relocated libby jar, and my compiled software:

![image](https://github.com/AlessioDP/libby/assets/29604693/93ec21e0-2a7e-473f-b483-5e5e5378b17c)

Yet, each time I get this error.

logs error ``` [23:25:20] [Server thread/INFO]: [LightLogin] Enabling LightLogin v1.0.0-SNAPSHOT [23:25:20] [Server thread/INFO]: [LightLogin] Loading library bcpkix-jdk18on [23:25:21] [Server thread/INFO]: [LightLogin] Downloaded library https://repo1.maven.org/maven2/org/bouncycastle/bcpkix-jdk18on/1.77/bcpkix-jdk18on-1.77.jar [23:25:21] [Server thread/INFO]: [LightLogin] Downloaded library https://repo1.maven.org/maven2/org/ow2/asm/asm-commons/9.2/asm-commons-9.2.jar [23:25:21] [Server thread/INFO]: [LightLogin] Downloaded library https://repo1.maven.org/maven2/org/ow2/asm/asm/9.2/asm-9.2.jar [23:25:21] [Server thread/INFO]: [LightLogin] Downloaded library https://repo1.maven.org/maven2/me/lucko/jar-relocator/1.7/jar-relocator-1.7.jar [23:25:21] [Server thread/INFO]: [LightLogin] Relocations applied to bcpkix-jdk18on-1.77.jar [23:25:21] [Server thread/INFO]: [LightLogin] Loading library HikariCP [23:25:21] [Server thread/INFO]: [LightLogin] Downloaded library https://repo1.maven.org/maven2/com/zaxxer/HikariCP/5.0.1/HikariCP-5.0.1.jar [23:25:21] [Server thread/INFO]: [LightLogin] Relocations applied to HikariCP-5.0.1.jar [23:25:21] [Server thread/INFO]: [LightLogin] Loaded config with language ENGLISH [23:25:21] [Server thread/ERROR]: Error occurred while enabling LightLogin v1.0.0-SNAPSHOT (Is it up to date?) java.lang.NoClassDefFoundError: top/cmarco/lightlogin/libs/com/zaxxer/hikari/HikariConfig at top.cmarco.lightlogin.I.A.(Unknown Source) ~[?:?] at top.cmarco.lightlogin.I.H.(Unknown Source) ~[?:?] at top.cmarco.lightlogin.LightLoginPlugin.G(Unknown Source) ~[?:?] at top.cmarco.lightlogin.LightLoginPlugin.onEnable(Unknown Source) ~[?:?] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:266) ~[spigot-api-1.20.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.20.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.20.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugin(CraftServer.java:541) ~[spigot-1.20.4-R0.1-SNAPSHOT.jar:4039-Spigot-c198da2-4c687f2] at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugins(CraftServer.java:455) ~[spigot-1.20.4-R0.1-SNAPSHOT.jar:4039-Spigot-c198da2-4c687f2] at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:220) ~[spigot-1.20.4-R0.1-SNAPSHOT.jar:4039-Spigot-c198da2-4c687f2] at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1000) ~[spigot-1.20.4-R0.1-SNAPSHOT.jar:4039-Spigot-c198da2-4c687f2] at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.20.4-R0.1-SNAPSHOT.jar:4039-Spigot-c198da2-4c687f2] at java.lang.Thread.run(Thread.java:840) ~[?:?] Caused by: java.lang.ClassNotFoundException: top.cmarco.lightlogin.libs.com.zaxxer.hikari.HikariConfig at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:160) ~[spigot-api-1.20.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:112) ~[spigot-api-1.20.4-R0.1-SNAPSHOT.jar:?] at java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[?:?] ... 13 more ```
AlessioDP commented 5 months ago

It seems that you are not accessing HikariCP from the isolated loader. If you isolate it, you can only access it via its ClassLoader and not from the global one. Try to remove the loaderId("hikari-id").isolatedLoad(true) part.

CMarcoo commented 5 months ago

Thanks I fixed my issues.

I think it would also be useful for other users to remind them that if their dependency has other compile dependencies, they need to also add them.

For instance, the cryptography and certificates dependency I'm using (bcpkix-jdk18on) also required "bcprov-jdk18on" and "bcutil-jdk18on".

It might be really useful for future users to tell them to always look at "compile dependencies" section of their libraries added from maven central.

Thanks for the help.