import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
BotStart.botStart();
}
}
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import java.util.EnumSet;
public class BotStart {
public static void botStart() {
String token = "(Bot Token)";
JDABuilder.createLight(token, EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT))
.addEventListeners(new BotListener())
.build();
}
}
plugins {
id("java")
}
group = "org.xeon.bot"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://oss.sonatype.org/content/repositories/central")
jcenter()
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly("net.dv8tion:JDA:5.0.1")
}
tasks.test {
useJUnitPlatform()
}
使用Gradle构建jar文件,Minecraft服务器日志中的错误:
[21:50:55 INFO]: [MinecraftDiscordBot] Enabling MinecraftDiscordBot v0.1*
[21:50:55 ERROR]: Error occurred while enabling MinecraftDiscordBot v0.1 (Is it up to date?)
java.lang.NoClassDefFoundError: net/dv8tion/jda/api/requests/GatewayIntent
at DiscordMinecraftBot-1.0-SNAPSHOT.jar/org.xeon.bot.Main.onEnable(Main.java:8) ~[DiscordMinecraftBot-1.0-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:288) ~[paper-api-1.21-R0.1-SNAPSHOT.jar:?]
at .....
Caused by: java.lang.ClassNotFoundException: net.dv8tion.jda.api.requests.GatewayIntent
at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:197) ~[paper-api-1.21-R0.1-SNAPSHOT.jar:?]
... 19 more
[21:50:55 INFO]: [MinecraftDiscordBot] Disabling MinecraftDiscordBot v0.1
在 Minecraft Java 插件上运行 JDA 时出现 NoClassDefFoundError。
我看到这个问题的原因在于依赖关系,但我找不到如何解决。
不能很好地使用英语。提前向您表示歉意。
您将 JDA 的依赖项包含为
compileOnly
,但执行时需要它。这就是它丢失并触发 ClassNotFoundException 的原因。按照 JDA文档的建议,将依赖项更改为
implementation
。
implementation("net.dv8tion:JDA:$version") // replace $version with the latest version