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>
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
B- Configuration Phase:
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".
Once the "Maven Dependency" has been added, please make sure its deploy Path value is "WEB-INF/lib".
C- Deploy and Runtime
right-click on your project -> maven install
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With