首先是我对JAVA AWS Eclipse Maven Tomcat的新手...我在尝试下面的代码时遇到以下错误。错误是" HTTP状态500 - java.lang.NoClassDefFoundError:无法初始化类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>
答案 0 :(得分:8)
java.lang.NoClassDefFoundError只是意味着:
&#34;嘿伙计,当你(自动)在Eclipse中构建项目时 (和/或在Maven中)(编译时间),您的IDE能够找到此类 com.amazonaws.services.sqs.AmazonSQSClient。但是当你想要继续前进的时候 服务器(运行时),我无法再找到它。&#34;
所以你错过了之前编译过的运行时类。
现在请这样做:
A-清洁阶段
B-配置阶段:
,右键单击 - &gt;部署大会。你会看到一种带有列的表格&#34; source&#34;和&#34;部署路径&#34;。如果没有来源和#34; Maven Dependency&#34;,请点击按钮添加 - >确认您的一行。 Java构建路径条目 - &gt;下一个按钮 - &gt; &#34; Maven Dependency&#34;。
一旦&#34; Maven Dependency&#34;已添加,请确保其部署路径值为&#34; WEB-INF / lib&#34;。
C-Deploy and Runtime
右键点击您的项目 - &gt; maven安装
右键点击您的项目 - &gt;以(或调试为)运行 - &gt;选择你tomcat而不是启动它。您的项目必须已经配置。
确保已安装eclipse插件m2e。它将使您在eclipse / maven中的开发生活更轻松。