我想获取SQS队列中的消息。我是第一次使用maven。以下是我到目前为止所做的步骤。
1。使用此命令创建maven项目:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=aws-try -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
上面的命令使用src文件夹和pom.xml创建了一个 aws-try 目录。
2。在pom.xml中添加了AWS-SDK依赖项:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.78</version>
</dependency>
第3。在src&gt;下添加了SQSTry.java文件主要&gt; java&gt; com&gt; mycompany&gt; app&gt; SQSTry.java
package com.mycompany.app;
import java.util.List;
import java.util.Map.Entry;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.DeleteQueueRequest;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.SendMessageRequest;
public class SQSTry {
public static void main (String args[]) {
System.out.println("SQSTry");
AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (~/.aws/credentials), and is in valid format.",
e);
}
AmazonSQS sqs = new AmazonSQSClient(credentials);
Region apNortheast1 = Region.getRegion(Regions.AP_NORTHEAST_1);
sqs.setRegion(apNortheast1);
System.out.println("===========================================");
System.out.println("Getting Started with Amazon SQS");
System.out.println("===========================================\n");
}
}
4。现在打包命令
mvn package
上面的命令是针对aws-try目录的根目录中的pom.xml运行的。
这会出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/AmazonClientException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.AmazonClientException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
我已正确添加了依赖项。如果你注意到上面的SQSTry.java文件,那么 AWSCredentials 也是来自amazon的包,但是id不会给出任何错误。
我错过了什么?
答案 0 :(得分:1)
你能试试mvn clean install
吗?还要验证您是否使用了适用于SDK的版本
尝试添加
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.78</version>
</dependency>
答案 1 :(得分:1)
您需要将 maven-shade-plugin 添加到pom.xml,它将所有AWS sdk jar打包到一个独立的jar文件中。
添加以下内容对我有用:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我从here找到了这个解决方案。