javanetbeanssmtp

Exception: java.lang.NoClassDefFoundError: javax/activation/DataSource when sending mail


I'm trying to send Mail using SMTP on NetBeans when I get the error message:

run: Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource at SendMail.createMessage(SendMail.java:45) at SendMail.main(SendMail.java:59) Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource 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 C:\Users\Admin\AppData\Local\NetBeans\Cache\12.0\executor-snippets\run.xml:111: The following error occurred while executing this line: C:\Users\Admin\AppData\Local\NetBeans\Cache\12.0\executor-snippets\run.xml:68: Java returned: 1 BUILD FAILED (total time: 0 seconds)

How do I need to fix it?

(I use Apache NetBeans IDE 12.0; Java: 15; Java HotSpot(TM) 64-Bit Server VM 15+36-156)


Solution

  • For classes such as javax.activation.DataSource you need the JAR file for the JavaBeans Activation Framework.

    You can download the JAR from Maven Central here - click on the jar link there.

    If you are using a dependency management tool such as Maven, Gradle, etc. then use the related configuration (also available in that same page). Using a dependency management tool is strongly recommended over just downloading JAR files one-by-one.


    You should also consider replacing your javax imports with jakarta imports, since that is now where these packages are maintained, going forward.

    If you do that, then you need to use the Jakarta Activation API, available here. For example:

    <dependency>
        <groupId>jakarta.activation</groupId>
        <artifactId>jakarta.activation-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    

    And if you do that, you should also replace JavaMail classes too - for example, you can replace this:

    import javax.mail.Message;
    

    with this:

    import jakarta.mail.Message;
    

    And use a Jakarta Mail resource, for example:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>jakarta.mail</artifactId>
        <version>2.0.1</version>
    </dependency>