sendgrid / sendgrid-java

The Official Twilio SendGrid Led, Community Driven Java API Library
https://sendgrid.com
MIT License
483 stars 407 forks source link

java.lang.ClassNotFoundException: com.sendgrid.helpers.mail.objects.Email #652

Open belezi opened 3 years ago

belezi commented 3 years ago

Issue Summary

On run time I always get classNotFoundException basically on every class from com.sendgrid package. Has anyone else run into this that could shed some light as to why this is happening? I am on the latest version 4.6.7

Any help is appreciated. TY

Code Snippet

import com...Notification;
import com...NotificationSendException;
import com...EmailNotificationClientInterface;
import com...NotificationResponse;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;

import java.io.IOException;

public class SendGridEmailNotificationService implements EmailNotificationClientInterface {

    @Override
    public NotificationResponse sendEmailNotification(Notification notification) throws NotificationSendException, IOException {
        Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
        Email from = new Email("test@example.com");
        String subject = "Sending with SendGrid is Fun";
        Email to = new Email("belezi@intelycare.com");
        Mail mail = new Mail(from, subject, to, content);
        SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
        Request request = new Request();

        Response response = new Response();

        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            response = sg.api(request);
            if (response.getStatusCode() == 200) {

            }
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            throw ex;
        }
        return this.processResponse(notification, response);
    }

    private NotificationResponse processResponse(Notification notification, Response response){
        NotificationResponse notificationResponse = new NotificationResponse();
        notificationResponse.setErred(notification.getErredNotification());
        notificationResponse.setSent(!notification.getErredNotification());
        return notificationResponse;
    }
}

pom.xml file

Code Snippet

<dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>java-http-client</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>sendgrid-java</artifactId>
            <version>4.6.7</version>
        </dependency>

Exception/Log

ERROR [2020-10-20 20:35:24,287] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 98ade0ddcde6ee9c
! java.lang.ClassNotFoundException: com.sendgrid.helpers.mail.objects.Content
! at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
! at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
! at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
! ... 64 common frames omitted
! Causing: java.lang.NoClassDefFoundError: com/sendgrid/helpers/mail/objects/Content

Technical details:

thinkingserious commented 3 years ago

Thank you for reporting this @belezi!

I was able to recreate in the following manner:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Added the dependencies to pom.xml:

    <dependency>
      <groupId>com.sendgrid</groupId>
      <artifactId>java-http-client</artifactId>
      <version>4.3.6</version>
    </dependency>
    <dependency>
      <groupId>com.sendgrid</groupId>
      <artifactId>sendgrid-java</artifactId>
      <version>4.6.7</version>
    </dependency>

Added the following code to App.java:

package com.mycompany.app;

import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.Method;
import java.io.IOException;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args ) throws IOException {
        Email from = new Email("example@example.com");
        String subject = "Sending with Twilio SendGrid is Fun";
        Email to = new Email("example@example.com");
        Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
        Mail mail = new Mail(from, subject, to, content);

        SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
        Request request = new Request();
        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            throw ex;
        }
    }
}
cd my-app
mvn package
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

DianeBustamante commented 3 years ago

I am having the exact same issue. This needs to be resolved ASAP in order to comply with SendGrid migrating away from username/password to API Keys.

DianeBustamante commented 3 years ago

NOTE: I was able to get this to work by updating the <build> section of my pom.xml file. I started with a pom file from a simple archetype. I updated it with the following <build> section:

   <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.sgtest.SendGridTest2.App</mainClass>
                        </transformer>
                    </transformers>
                    <!-- exclude signed Manifests -->
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.sgtest.SendGridTest2.App</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./public/</Class-Path>
                        </manifestEntries>                       
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
zionc44 commented 3 years ago

I am having the exact same issue but I am not using maven, I was just downloading the JAR file and on run time I always get classNotFoundException basically on every class from com.sendgrid package.

Is there any solution for me?

DianeBustamante commented 3 years ago

I was able to get this example to work without maven by including two libraries: sendgrid-java-4.6.7.jar and java-http-client-latest.jar. I believe I downloaded the sendgrid-java-4.6.7.jar from the Maven repository. A link to java-http-client-latest.jar can be found at https://github.com/sendgrid/sendgrid-java. I believe you also need to include three jackson libraries: jackson-annotation-2.9.0.jar, jackson-core-2.9.2.jar and jackson-databind-2.9.2.jar. I hope this helps.

zionc44 commented 3 years ago

I was able to get this example to work without maven by including two libraries: sendgrid-java-4.6.7.jar and java-http-client-latest.jar. I believe I downloaded the sendgrid-java-4.6.7.jar from the Maven repository. A link to java-http-client-latest.jar can be found at https://github.com/sendgrid/sendgrid-java. I believe you also need to include three jackson libraries: jackson-annotation-2.9.0.jar, jackson-core-2.9.2.jar and jackson-databind-2.9.2.jar. I hope this helps.

Thanks, I have download sendgrid-java-4.6.7.jar and all the dependencies JAR base on the docs at maven repository at the https://mvnrepository.com/artifact/com.sendgrid/sendgrid-java/4.6.7 but still getting the same runtime classNotFoundException for any class from com.sendgrid.

any other idea?

DianeBustamante commented 3 years ago

I did not use the java-http-client library from the maven repository. As I stated above, I went to https://github.com/sendgrid/sendgrid-java and followed the java-http-client link on that page. It led me to the jar named java-http-client-latest.jar. Try that.

zionc44 commented 3 years ago

I did not use the java-http-client library from the maven repository. As I stated above, I went to https://github.com/sendgrid/sendgrid-java and followed the java-http-client link on that page. It led me to the jar named java-http-client-latest.jar. Try that.

thanks again, I have tried but still i am keeping getting the Class Not Found Exception.

DianeBustamante commented 3 years ago

I don't know what else to tell you. I am using java 1.8, specifically jre.1.8.0_271. Don't know if that could impact your build.

priyav26 commented 3 years ago

Any solution for this? I am facing the same problem. I tried to execute it through eclipse ide (Gradle). I get the below exception

Exception in thread "main" java.lang.NoClassDefFoundError: com/sendgrid/Email at SendGridEmailer.main(SendGridEmailer.java:10) Caused by: java.lang.ClassNotFoundException: com.sendgrid.Email 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) ... 1 more

kamalbahadur commented 3 years ago

I had the same issue. For me, the problem was that maven was pulling older version of sendgrid-java even though 4.7.2 was the transitive dependency. So the fix was to add the dependency 4.7.2 directly in pom.xml and not depend on transitive dependency.