Search code examples
javajavafxnoclassdeffounderrorsendgrid

API works with Java application but not JavaFX


I am building a software in Java and JavaFX. It generates PDF reports (with DynamicReports) then uses SendGrid API to send to send them. I have already added the SendGrid API .jar file to the classpath.

The problem is that my JavaFX application crashes when I instanciate my subclass of SengGrid. As you can see:

Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: com/sendgrid/SendGrid$Email
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)  at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at Reporting.TableReport.lambda$build$0(TableReport.java:160)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.sendgrid.SendGrid$Email
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 20 more

Now the interesting part is that the API works perfectly when I call it from a non JavaFX project. And more interestingly, it also works when I call it from a "console" main class inside the very same JavaFX project.

So far, I have tried to call that "console" main from my JavaFX code as a sort of static method that would fulfill my task (sending the PDF files). But again, I encounter the very same exception.

Can anyone suggest a solution to solve this ? (or at least go around it)


Solution

  • Turns out it was an issue with Netbeans. Using the exact same code with Eclipse works perfectly. I couldn't find an explanation but I'm giving this answer for anyone who might stumble upon the same issue as me.