NoClassDefFoundError: com/google/auth/Credentials occuring after jar compilation, but not in dev environment

404 Views Asked by At

Background

I have built a mod for Minecraft, and I am now at the point where it is ready to be built into a jar and run it in the game. My IDE is Intellij, the framework I am using is Fabric, and I am using Gradle as the build tool. I am also using some google api's in my mod, brought in as dependencies in my build.gradle file.

Problem

The mod works as intended when I boot it up through my IDE. However, when I build the mod into a jar, and put the mod in the mod folder such that the actual Minecraft exe can load it, the game crashes when I begin one of the processes in the mod that uses a google api call. It crashes due to the following exception:

java.lang.NoClassDefFoundError: com/google/auth/Credentials

In this block of code (specifically at the first reference to the GoogleCredentials class):

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();
        }

I have these Google imports in that class:

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;

Here's the dependencies in my build.gradle file:

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"
}

Now, I have pretty much no idea why this is happening. As you can see, the authentication library from Google is present here, and it builds and runs perfectly fine in my development environment. I do not understand why it can't resolve the dependency after it has been built into a .jar file.

The only 'lead' that I have on the issue is that, one of these other Google dependencies may have the authentication library as a dependency, so maybe that is crossing some wires? Not sure. Help would be very much appreciated...

1

There are 1 best solutions below

0
Truthy_dev On

I assume you use the default build.gradle, i would recommend switching to fabric loom.

https://github.com/FabricMC/fabric-loom

I hope this helps :) had the same issue a while ago.