未找到类异常:亚马逊在线认证- java

dl5txlt9  于 2023-02-18  发布在  Java
关注(0)|答案(2)|浏览(126)

我有/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

pbpqsu0x

pbpqsu0x1#

我想您还应该在/aws文件夹中包含aws-java-sdk-core-1.9.23.jar
com.amazonaws.auth.AWSCredentialscom.amazonaws.auth.BasicAWSCredentials驻留在该特定jar中。
希望有帮助。
谢谢。

xam8gpfp

xam8gpfp2#

在2023年,这可能是由于TestContainers "错误"导致的,在您使用AWS SDK 2.x时,TestContainers通过其自己的API从AWS SDK 1.x核心中暴露类
参见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>

相关问题