![](/img/trans.png)
[英]Could not initialize class com.amazonaws.partitions.PartitionsLoader
[英]Could not initialize class com.amazonaws.services.sqs.AmazonSQSClient
首先,我是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>
java.lang.NoClassDefFoundError只是意味着:
“嘿伙計,當你(自動)在Eclipse(和/或Maven)中構建項目( 編譯時 )時,你的IDE能夠找到這個類com.amazonaws.services.sqs.AmazonSQSClient。但是當你想要運行時在服務器上( 運行時 ),我找不到它了。“
所以你在運行時缺少一個以前編譯過的類。
現在請這樣做:
A-清潔階段
B-配置階段 :
在你的eclipse項目中,右鍵單擊 - >部署程序集。 您將看到一種包含“source”和“Deploy Path”列的表。 如果沒有源“Maven Dependency”的行,請點擊按鈕添加 - > Java Build path Entries - > next button - >“Maven Dependency”。
添加“Maven依賴關系”后,請確保其部署路徑值為“WEB-INF / lib”。
C-部署和運行時
右鍵單擊您的項目 - > maven install
右鍵單擊您的項目 - >運行為(或調試為) - >選擇您的tomcat而不是啟動它。 那時你的項目必須已經配置好了。
確保你已經安裝了eclipse插件m2e。 它將使您在eclipse / maven中的開發生活更輕松。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.