Failed to initialize class com.amazonaws.services.sqs.AmazonSQSClient

First of all, Im new for JAVA AWS Eclipse Maven Tomcat ... I get the following error when trying to execute the following code. Error: "HTTP Status 500 - java.lang.NoClassDefFoundError: failed to 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> 

+6
source share
1 answer

java.lang.NoClassDefFoundError simply means:

"Hi dude, when you (automatically) created your project in Eclipse (and / or in Maven) (compilation time), your IDE could find this class com.amazonaws.services.sqs.AmazonSQSClient. But when you want to start the server (runtime), I can’t find it anymore. "

therefore, you are missing a class at runtime that was compiled earlier.

Now do the following:

A- Cleaning Phase

  • in eclipse go to Menubar -> clean -> clean the whole project.
  • If eclipse does not yet support maven (for example, does not have m2e): open your command prompt (Windows or Linux or something else) and go to the directory containing the eclipse project and enter "mvn clean".
  • If eclipse supports m2e, then directly in eclipse in the project, right-click β†’ maven β†’ update project.

B- Configuration phase :

  1. in your eclipse project, right click -> Assembly Deployment. You will see some kind of table with the columns "source" and "deployment path". if there is no line with the source "Maven Dependency", please make sure you click add β†’ Java Build path Entries β†’ next button β†’ "Maven Dependency".

  2. After adding "Maven Dependency", make sure that its deployment value is "WEB-INF / lib".

C- Deployment and Execution

  1. right click on your project -> maven install

  2. right click on your project -> run (or debug as) -> select you tomcat and run it. Your project should be set up by then.

make sure you install the eclipse m2e plugin. This will make your life easier in Eclipse / Maven.

+7
source

Source: https://habr.com/ru/post/974884/


All Articles