java - 无法初始化类 com.amazonaws.services.sqs.AmazonSQSClient

标签 java eclipse maven tomcat amazon-web-services

首先,我是 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 简单的意思是:

"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."

所以你在运行时缺少一个之前编译过的类。

现在请这样做:

A-清洁阶段

  1. 在 Eclipse 中转到菜单栏 -> 清理 -> 清理所有项目。
  2. 如果 eclipse 还不支持 maven(例如没有 m2e):打开命令行(Windows 或 Linux 或其他)并移动到包含您的 eclipse 项目的目录并键入“mvn clean”。
  3. 如果 eclipse 确实支持 m2e,那么直接在 eclipse 中的项目上右键单击 -> maven -> 更新项目。

B-配置阶段:

  1. 在您的 eclipse 项目中,右键单击 -> Deployment Assembly。您会看到一种包含“源”和“部署路径”列的表格。如果没有源“Maven Dependency”的行,请通过单击按钮添加 -> Java 构建路径条目 -> 下一个按钮 ->“Maven 依赖项”来确保你的行。

  2. 添加“Maven 依赖”后,请确保其部署路径值为“WEB-INF/lib”。

C-部署和运行时

  1. 右键单击您的项目 -> maven install

  2. 右键单击您的项目 -> 运行方式(或调试方式)-> 选择您的 tomcat 然后启动它。那时您的项目必须已经配置好。

确保您已经安装了 eclipse 插件 m2e。它将使您在 eclipse/maven 中的开发生活更加轻松。

关于java - 无法初始化类 com.amazonaws.services.sqs.AmazonSQSClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25684905/

相关文章:

java - 使用 Maven 创建一个包含已编译类和 javadoc 的 jar

java - C# 和 Java - 将文件从 Android 上传到 WCF

java - 在同一个类中同时使用继承和组合?

java - java实现tomcat部署war

Eclipse 窗口不会停靠在 Ubuntu 上

java - eclipse 调试器中的重复值

java - Red5服务器安装不完整源码

java restful 服务作为 jar 对 war 的依赖

java - 在 pom.xml 中指定换行符

java - 如何处理java.lang.OutOfMemoryError : GC overhead limit exceeded gracefully?