0

💻 Environment:

Java Version: Corretto 17.0.8

Google Cloud KMS library: google-cloud-kms:2.38.0

Build Tool: Maven

IDE: IntelliJ IDEA Community Edition

📦 Dependencies (pom.xml):

<dependencies>
 <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-kms</artifactId>
    <version>2.38.0</version>
</dependency>

 <dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>4.0.0-rc-2</version>
 </dependency>

 <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.9</version>
 </dependency>
</dependencies>

Code Sample:

import com.google.cloud.kms.v1.*;
import com.google.protobuf.ByteString;

import java.io.IOException;
import java.util.Base64;

public class KmsExample {
    public static void main(String[] args) throws IOException {
        final var projectId = "my-project";
        final var locationId = "global";
        final var keyRingId = "my-keyring";
        final var keyId = "my-key";

        CryptoKeyName keyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

        String plaintext = """
            {
                "pan": "1234567890123456",
                "cvv": "123",
                "expiry": "12/30"
            }
            """;

        try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
            EncryptResponse encryptResponse = client.encrypt(keyName, ByteString.copyFromUtf8(plaintext));
            String encryptedBase64 = Base64.getEncoder().encodeToString(encryptResponse.getCiphertext().toByteArray());
            System.out.println("Encrypted (Base64): " + encryptedBase64);

            DecryptResponse decryptResponse = client.decrypt(keyName, encryptResponse.getCiphertext());
            String decrypted = decryptResponse.getPlaintext().toStringUtf8();
            System.out.println("Decrypted: " + decrypted);
        }
    }
}

Error Stack Trace:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/protobuf/MapFieldReflectionAccessor
    at com.google.cloud.kms.v1.stub.GrpcKeyManagementServiceStub.<clinit>(GrpcKeyManagementServiceStub.java:159)
    at com.google.cloud.kms.v1.stub.KeyManagementServiceStubSettings.createStub(KeyManagementServiceStubSettings.java:687)
    at com.google.cloud.kms.v1.KeyManagementServiceClient.<init>(KeyManagementServiceClient.java:783)
    at com.google.cloud.kms.v1.KeyManagementServiceClient.create(KeyManagementServiceClient.java:765)
    at com.example.KmsExample.main(KmsExample.java:31)
Caused by: java.lang.ClassNotFoundException: com.google.protobuf.MapFieldReflectionAccessor
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    ... 6 more

Any guidance on how to resolve this class loading issue would be appreciated!

1
  • 1
    Don't use 4.0.0-rc-2 (see comment). Use either 4.31.1 latest release or 3.25.8 if you need a release prior to the abandoned 4.0.0 Commented May 30 at 14:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.