尝试使用 mongodb 时出现 NoClassDefFoundError - Spigot 插件

NoClassDefFoundError while trying to use mongodb - Spigot Plugin

我想编写一个可以通过检查 nosql 数据库来验证用户身份的插件,但是当我尝试在我的服务器中 运行 它时遇到错误。我使用 gradle 导入 mongo 驱动程序,但得到 NoClassDefFoundError。这是错误代码:

[04:17:15] [Server thread/INFO]: [mcloginx] mongo uri: mongodb://localhost:27017
[04:17:15] [Server thread/WARN]: java.lang.NoClassDefFoundError: com/mongodb/MongoClient
[04:17:15] [Server thread/WARN]:        at com.t2o0n321.mcloginx.utils.db.testConnect(db.java:21)
[04:17:15] [Server thread/WARN]:        at com.t2o0n321.mcloginx.mcloginx.onEnable(mcloginx.java:35)
[04:17:15] [Server thread/WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263)
[04:17:15] [Server thread/WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342)
[04:17:15] [Server thread/WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480)
[04:17:15] [Server thread/WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:492)
[04:17:15] [Server thread/WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:406)
[04:17:15] [Server thread/WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.reload(CraftServer.java:879)
[04:17:15] [Server thread/WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:651)
[04:17:15] [Server thread/WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27)
[04:17:15] [Server thread/WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149)
[04:17:15] [Server thread/WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:761)
[04:17:15] [Server thread/WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:746)
[04:17:15] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:426)
[04:17:15] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:395)
[04:17:15] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1127)
[04:17:15] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:966)
[04:17:15] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda[=11=](MinecraftServer.java:273)
[04:17:15] [Server thread/WARN]:        at java.base/java.lang.Thread.run(Thread.java:829)
[04:17:15] [Server thread/WARN]: Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient
[04:17:15] [Server thread/WARN]:        at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:140)
[04:17:15] [Server thread/WARN]:        at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:99)
[04:17:15] [Server thread/WARN]:        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
[04:17:15] [Server thread/WARN]:        ... 19 more
[04:17:15] [Server thread/INFO]: [mcloginx] Can't connect to mongo

这是我的 build.gradle 文件:

plugins {
    id 'java'
    id 'application'
}

group 'com.t2o0n321.mcloginx'
version '1.0.0'

repositories {
    mavenCentral()
    maven {url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'}
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
    // implementation 'org.mongodb:mongodb-driver-core'
    implementation 'org.mongodb:mongodb-driver-core:4.4.1'
    implementation 'org.mongodb:mongo-java-driver:3.12.10'
}

test {
    useJUnitPlatform()
}

我用谷歌搜索了它,但还没有得到解决方案。 如何解决问题?

错误是因为没有导出带有所有依赖项的jar。 我通过使用 shadowJar 将所有依赖项打包在一起解决了这个问题。

将以下代码添加到build.gradle

plugins {
    id 'com.github.johnrengelman.shadow' version '7.0.0'
}
project.setProperty("mainClassName", "com.your.mainclass")
shadowJar {
    manifest {
        attributes 'Main-Class': 'com.your.mainclass'
    }
}

然后输入 ./gradlew shadowJar int 终端。

您将在 build/libs 目录中找到 jar 文件。