Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not initialize class com.amazonaws.services.sqs.AmazonSQSClient

First of all Im new to JAVA AWS Eclipse Maven Tomcat... Im getting the following error while trying following code.. Error is "HTTP Status 500 - java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.services.sqs.AmazonSQSClient"...

package sms.pii.webservice;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.*;

public class AWSSimpleQueueServiceUtil {

public BasicAWSCredentials credentials;
public AmazonSQS sqs;


public AWSSimpleQueueServiceUtil(){
    try{
        String accessKey= "xxxxxx";
        String secretKey= "xxxxxxxx";
        this.credentials = new BasicAWSCredentials(accessKey,secretKey);
        this.sqs = new AmazonSQSClient(this.credentials);
        //this.sqs.setEndpoint("https://sqs.ap-southeast-1.amazonaws.com");

    }
    catch(Exception e){
        System.out.println("exception while creating awss3client : " + e);
    }
}

public String createNewQueue(String queueName){
    CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
    String queueUrl = this.sqs.createQueue(createQueueRequest).getQueueUrl();
    return queueUrl;
}

public String getQueueUrlByName(String queueName){
    GetQueueUrlRequest getQueueUrlRequest = new GetQueueUrlRequest(queueName);
    return this.sqs.getQueueUrl(getQueueUrlRequest).getQueueUrl();
}

public ListQueuesResult listAllQueues(){
   return this.sqs.listQueues();
}

}

package sms.pii.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import sms.pii.webservice.AWSSimpleQueueServiceUtil; 

@Path("/Queue")
public class TestSQS {

@GET
@Path("/Name/{name}")
@Produces(MediaType.APPLICATION_JSON)
public Student produceJSON( @PathParam("name") String name ) {
    Student st = new Student(name, "kumar",55,21);
    return st;
}

@GET
@Path("/createQueue/{name}")
@Produces(MediaType.TEXT_PLAIN)
public String createQueue(@PathParam("name") String queueName){
    AWSSimpleQueueServiceUtil test = new AWSSimpleQueueServiceUtil();
    return test.createNewQueue(queueName);
}

@GET
@Path("/getQueueUrl/{name}")
@Produces(MediaType.TEXT_PLAIN)
public String getQueueUrl(@PathParam("name") String queueName){
    AWSSimpleQueueServiceUtil test = new AWSSimpleQueueServiceUtil();
    return test.getQueueUrlByName(queueName);
}
}

pom.xml

<dependencies>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.8.9.1</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

like image 633
Naveenkumar Avatar asked Sep 05 '14 11:09

Naveenkumar


1 Answers

java.lang.NoClassDefFoundError simply means:

"Hey dude, when you (automatically) built your project in Eclipse (and/or in Maven) (compilation time), your IDE was able to find this class com.amazonaws.services.sqs.AmazonSQSClient. But when you want to run on the server (runtime) , I can't find it any-more."

so you are missing a class at runtime which was compiled before.

Now please do this:

A- Cleaning phase

  1. in eclipse go to Menubar -> clean -> clean all project.
  2. if eclipse doesn't support maven yet (e.g not having m2e): open your command line (Windows or Linux or whatever) and move to the directory which contains you eclipse project and type "mvn clean".
  3. if eclipse does support m2e then, directly in eclipse on your project right-click -> maven -> update project.

B- Configuration Phase:

  1. in your eclipse project, right-click -> Deployment Assembly. You'll see a kind of table with columns "source" and "Deploy Path". if there no row with source "Maven Dependency", please make sure your one by click the button add -> Java Build path Entries -> next button -> "Maven Dependency".

  2. Once the "Maven Dependency" has been added, please make sure its deploy Path value is "WEB-INF/lib".

C- Deploy and Runtime

  1. right-click on your project -> maven install

  2. right-click on your project -> run as (or debug as) -> select you tomcat and than start it. your project must be have been configured by then.

make sure you have installed the eclipse plugin m2e. It 'll make your development life in eclipse/maven easier.

like image 150
arthur Avatar answered Oct 29 '22 10:10

arthur