1

I want to fetch the messages in the SQS queue. I am using the maven for the first time. Here are the steps I have did so far.

1. Created maven project using this command:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=aws-try -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The above command created a aws-try directory with src folder and pom.xml.

2. Added AWS-SDK dependency in pom.xml:

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk</artifactId>
  <version>1.11.78</version>
</dependency>

3. Added the SQSTry.java file under src > main > java > com > mycompany > app > 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. Now package command

mvn package

The above command was run against the pom.xml in the root of aws-try directory.

This gives the following error:

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

I have added the dependency correctly. If you have noticed the above SQSTry.java file, the AWSCredentials was also a package from amazon, but id does not give any error.

What am I missing ?

Lakshman Diwaakar
  • 7,207
  • 6
  • 47
  • 81
  • check this out, http://stackoverflow.com/questions/12811392/java-classnotfoundexception-with-maven-dependency – Shivakumar ss Jan 16 '17 at 04:12
  • @ShivaKumarSS Even after adding the scope with compile to dependency, it didnt work. I just cant understand, why it does not import aws sdk correctly. – Lakshman Diwaakar Jan 16 '17 at 05:32
  • "mvn package" will not run the main program. I see the exception is thrown while running main program. what is the exact command you are trying to use and what is your requirement. – Shivakumar ss Jan 16 '17 at 06:41
  • Ya "mvn package" is used to compile and give it a jar. I run the main program with this command. **java -cp com.company.app.**. I found out the culprit, it was not including a plugin. See the answer below. – Lakshman Diwaakar Jan 16 '17 at 06:53
  • good to hear that issue is identified. – Shivakumar ss Jan 16 '17 at 08:21

2 Answers2

6

You need to add maven-shade-plugin to the pom.xml which packages all AWS sdk jars to a standalone jar file.

Adding the following worked for me:

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

I found this solution from here.

Community
  • 1
  • 1
Lakshman Diwaakar
  • 7,207
  • 6
  • 47
  • 81
2

Can you try mvn clean install. Also verify if you are using the right version for the SDK

Try adding

<!-- 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>
cheseaux
  • 5,187
  • 31
  • 50
letmesolve
  • 264
  • 3
  • 8
  • Yes I have tried the clean install command too. Didn't work out. And I have cross checked the version too. The latest one – Lakshman Diwaakar Jan 16 '17 at 03:42
  • Changing the **aws-java-sdk** to **aws-java-sdk-core** gives error at the **mvn clean install** command execution. But changing back to **aws-java-sdk** gives no error during **mvn clean install** command execution, but gives ClassNotFound exception at final execution – Lakshman Diwaakar Jan 16 '17 at 04:40
  • Can you check whether aws-java-sdk-core jar is downloaded in your project or not ? class for which you are getting the exception is part of this jar. – Amit Jan 16 '17 at 06:17