背景
我已经为 Minecraft 构建了一个 mod,现在我已经准备好将其构建到 jar 中并在游戏中运行它。我的IDE是Intellij,我使用的框架是Fabric,我使用Gradle作为构建工具。我还在我的 mod 中使用了一些 google api,作为依赖项引入到我的 build.gradle 文件中。
问题
当我通过 IDE 启动该 mod 时,它会按预期工作。然而,当我将 mod 构建到 jar 中,并将 mod 放入 mod 文件夹中,以便实际的 Minecraft exe 可以加载它时,当我开始 mod 中使用 google api 调用的进程之一时,游戏崩溃了。由于以下异常而崩溃:
java.lang.NoClassDefFoundError: com/google/auth/Credentials
在此代码块中(特别是在第一次引用 GoogleCredentials 类时):
String credentials_path = GOOGLE_API_KEY_PATH_STRING;
GoogleCredentials credentials = null;
try (FileInputStream serviceAccountStream = new FileInputStream(credentials_path)) {
credentials = GoogleCredentials.fromStream(serviceAccountStream);
} catch (Exception e) {
e.printStackTrace();
}
SpeechSettings settings = null;
try {
settings = SpeechSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
} catch (IOException e) {
e.printStackTrace();
}
我在该类中有这些 Google 导入:
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.speech.v1.*;
import com.google.protobuf.ByteString;
这是我的 build.gradle 文件中的依赖项:
dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
implementation 'io.github.cdimascio:java-dotenv:5.2.2'
implementation 'com.googlecode.soundlibs:tritonus-share:0.3.7.4'
implementation 'com.googlecode.soundlibs:tritonus-all:0.3.7.2'
implementation 'com.googlecode.soundlibs:jlayer:1.0.1.4'
implementation 'com.googlecode.soundlibs:jorbis:0.0.17.4'
implementation 'com.googlecode.soundlibs:vorbisspi:1.0.3.3'
implementation 'com.googlecode.soundlibs:basicplayer:3.0.0.0'
implementation 'com.google.guava:guava:24.1-jre'
implementation 'org.threeten:threetenbp:1.3.6'
implementation 'com.google.http-client:google-http-client:1.22.0'
implementation 'com.google.cloud:google-cloud-texttospeech:2.18.0'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.16.0'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'
implementation 'org.slf4j:slf4j-api:1.7.32'
implementation 'com.google.code.gson:gson:2.10.1'
modRuntimeOnly "maven.modrinth:simple-voice-chat:fabric-1.20.1-2.4.24"
}
现在,我几乎不知道为什么会发生这种情况。正如您所看到的,来自 Google 的身份验证库出现在这里,并且它在我的开发环境中构建和运行得非常好。我不明白为什么它构建成 .jar 文件后无法解决依赖关系。
我在这个问题上唯一的“线索”是,这些其他 Google 依赖项之一可能将身份验证库作为依赖项,所以也许这跨越了一些线路?没有把握。非常感谢您的帮助...
我假设你使用默认的 build.gradle,我建议切换到 arch loom。
https://github.com/architectury/architectury-loom
导入 build.gradle 作为项目 让一切正常导入
打开 gradle 选项卡(右上角) 打开设置 > Gradle JVM > Java 17
打开文件 > 项目结构 将项目 SDK 和语言级别设置为 Java 1.8 / 8
我希望这会有所帮助:)不久前也遇到过同样的问题。