我的build.gradle
:
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile 'com.amazonaws:aws-java-sdk:1.11.171'
}
我的Java文件:
import com.amazonaws.services.kms.AWSKMS;
import com.amazonaws.services.kms.AWSKMSClientBuilder;
import com.amazonaws.services.kms.model.GenerateDataKeyRequest;
import com.amazonaws.services.kms.model.GenerateDataKeyResult;
import java.nio.charset.StandardCharsets;
public class KmsExample {
private static final String KEYID = "arn:aws:kms:us-east-1:[userid]:key/[cmk id]";
public static void main(String[] args) {
AWSKMS kms = AWSKMSClientBuilder.defaultClient();
GenerateDataKeyRequest dataKeyRequest = new GenerateDataKeyRequest();
dataKeyRequest.setKeyId(KEYID);
dataKeyRequest.setKeySpec("AES_128");
GenerateDataKeyResult dataKeyResult = kms.generateDataKey(dataKeyRequest);
String plaintextKey = StandardCharsets.UTF_8.decode(dataKeyResult.getPlaintext()).toString();
String encryptedKey = StandardCharsets.UTF_8.decode(dataKeyResult.getCiphertextBlob()).toString();
System.out.println(plaintextKey + " | " + encryptedKey);
}
}
足够简单。但是,当我运行我的文件时,它会给我NoClassDefFoundError
错误:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/amazonaws/services/kms/AWSKMSClientBuilder
at KmsExample.main(KmsExample.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException:
com.amazonaws.services.kms.AWSKMSClientBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
在IntelliJ中,我看到我的文件导入正常,所有JARS都在那里:
跳转到文件声明的Ctrl / Cmd + B 工作正常,证明文件在那里。
那么为什么我仍然会得到NoClassDefFoundError?
答案 0 :(得分:2)
您缺少包含AWSKMSClientBuilder
类的依赖项。只需将以下内容添加到build.gradle
:
compile group: 'com.amazonaws', name: 'aws-java-sdk-kms', version: '1.11.171'