Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException: com.amazonaws.auth.AWSCredentials - java

I have /aws folder with aws jars and my main program Aws.class aws is also a package:

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 program:

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
}

}

when calling the program by going one level up and isseuing a command: java aws.Aws

i'm getting an error:

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)  

classpath is

.:/aws

like image 431
Snyger Avatar asked Mar 13 '15 03:03

Snyger


1 Answers

I guess you should include aws-java-sdk-core-1.9.23.jar also in your /aws folder.

com.amazonaws.auth.AWSCredentials and com.amazonaws.auth.BasicAWSCredentials resides in that particular jar.

Hope it helps.

Thanks.

like image 182
Rahul Kumar Avatar answered Sep 22 '22 18:09

Rahul Kumar