Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

I have created a maven project using the below command:

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

And then I added AWS SDK dependency in pom.xml:

<dependency>

  <groupId>com.amazonaws</groupId>

  <artifactId>aws-java-sdk</artifactId>

  <version>1.11.78</version>

</dependency>

Then added SQStry.java file:

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");

}

}

And for package:

mvn package

And this the error that I received:

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

What I'm missing?

1 Answer

0 votes
by (12.4k points)

Here you need to add the "maven-shade-plugin" to the pom.yml which allows AWS SDK jar to a standalone jar file.

You can refer to the below code and make changes as per your requirements:

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

You can check out our comprehensive AWS Training to learn all about AWS.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...