gpt4 book ai didi

java - ClassNotFoundException : com. amazonaws.auth.AWSCredentials - java

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:32 27 4
gpt4 key购买 nike

我有/aws 文件夹,其中包含 aws jar 和我的主程序 Aws.class aws 也是一个包:

spectjrt.jar                           
aspectjweaver.jar
aws-java-sdk-1.9.23-javadoc.jar
aws-java-sdk-1.9.23-sources.jar
aws-java-sdk-1.9.23.jar
aws-java-sdk-flow-build-tools-1.9.23.jar
Aws.class
commons-codec-1.6.jar
commons-logging-1.1.3.jar
freemarker-2.3.18.jar
httpclient-4.3.jar
httpcore-4.3.jar
jackson-annotations-2.3.0.jar
jackson-core-2.3.2.jar
jackson-databind-2.3.2.jar
javax.mail-api-1.4.6.jar
joda-time-2.2.jar
namast1.csv
spring-beans-3.0.7.jar
spring-context-3.0.7.jar
spring-core-3.0.7.jar

AWS.java 程序:

package aws;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
import java.io.IOException;


public class Aws {

private static final String USERNAME = "xxx”;
private static final String PASSWORD = "yyy";
private static final String FILEPATH = "/aws/";

private static final String bucketName = "bucket";
private static final String keyName = "name";



/**
* @param args the command line arguments
*/
public static void main(String[] args) {

//String fil = args[1];
// StringBuilder filename = new StringBuilder(FILEPATH);

String filename = "/aws/names.csv";



File filen = new File(filename.toString());


AWSCredentials credentials = new BasicAWSCredentials("xxxx", "yyyy");
AmazonS3Client clnt = new AmazonS3Client(credentials);


try {

System.out.println("Uploading a new object to S3");


clnt.putObject(new PutObjectRequest(
bucketName, keyName, filen));

}

catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which " +
"means your request made it " +
"to Amazon S3, but was rejected with an error response" +
" for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which " +
"means the client encountered " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}




// TODO code application logic here
}

}

通过向上一级并发出命令来调用程序时:java aws.Aws

我收到错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com.amazonaws.auth.AWSCredentials       
at java.lang.J9VMInternals.verifyImpl(Native Method)
at java.lang.J9VMInternals.verify(J9VMInternals.java:94)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:171)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:703)
at java.lang.ClassLoader.loadClass(ClassLoader.java:682)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:665)

类路径是

.:/aws

最佳答案

到 2023 年,这可能是由于 TestContainers“bug”造成的,当您使用 AWS SDK 2.x 时,它会通过自己的 API 公开来自 AWS SDK 1.x Core 的类

参见https://github.com/testcontainers/testcontainers-java/issues/1442

解决方法是将 1.x 也包含为测试依赖项。

        <dependency> <!-- Localstack depends on 1.x internally. -->
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.12.402</version>
<scope>test</scope>
<exclusions><exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion></exclusions>
</dependency>

关于java - ClassNotFoundException : com. amazonaws.auth.AWSCredentials - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29024520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com