0

I've just built a maven project (Sending message using AWS SNS) in intelIJ and it worked fine after that i built the same maven project using CLI but ended up in ClassNotFoundException (for the API class of AWS) i'm facing this for a long time. Any help will be really helpful. Thanks in advance.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rmi</groupId>
    <artifactId>AwsSNSClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-bom</artifactId>
      <version>1.11.327</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sns</artifactId>
            <!--<version>1.11.979</version>-->
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

Above is my pom.xml file And the following is my Java Code

import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.MessageAttributeValue;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;

import java.util.HashMap;
import java.util.Map;

public class AwsSNSClient
{
    public static final String AWS_ACCESS_KEY_ID = "aws.accessKeyId";
    public static final String AWS_ACCESS_KEY = "aws.secretKey";
    static
    {
        System.setProperty(AWS_ACCESS_KEY_ID,"xxx");
        System.setProperty(AWS_ACCESS_KEY,"yyy");
    }
    public static void main(String args[])
    {
        AwsSNSClient myClient = new AwsSNSClient();
        myClient.sendSingleSMS("This is a message sent from PC from MAVEN CLI","+91xxx");
    }
    public void sendSingleSMS(String message,String phoneNumber)
    {
        AmazonSNS snsClient = AmazonSNSClient.builder().withRegion(Regions.AP_SOUTH_1).build();
        Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
        smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue("MyWebsite").withDataType("String"));
        smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));

        PublishResult result = snsClient.publish(new PublishRequest()
                .withMessage(message)
                .withPhoneNumber(phoneNumber)
                .withMessageAttributes(smsAttributes));

        System.out.println("Message Sent Successfully---"+result.getMessageId());
    }
}

And here is the Exception generated in my Command Line

Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/services/sns/AmazonSNSClient
        at com.rmi.AwsSNSClient.sendSingleSMS(AwsSNSClient.java:29)
        at com.rmi.AwsSNSClient.main(AwsSNSClient.java:25)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.sns.AmazonSNSClient
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 2 more

0 Answers0