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

2

9

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)

Nazarene answered 11/11, 2021 at 14:30 Comment(0)
D
15

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>
Dexterous answered 11/11, 2021 at 16:43 Comment(5)
I solved the problem, thank you!Nazarene
The link to Maven points to old Javax Activation. The new link seems to be mvnrepository.com/artifact/com.sun.activation/…Shier
Thank you for your comment @JustAMartin. The Jakarta Activation link in the answer points to where the latest versions are maintained in Maven (currently 2.1.0). The link in your comment only includes Jakarta versions up to 2.0.1 - it still references com.sun.activation in the URL - I think that was just a transitional Maven location for the libraries. Now, everything is located under jakarta.activation. At least, that is my understanding - I could be wrong.Dexterous
Try the link in your text "You can download the JAR from Maven Central here" - when clicking on "here" it leads to old version 1.1.1 for some reason.Shier
Yes - that is deliberate, because the question was using javax libraries. So that link is for the missing javax activation JAR, to directly answer the question. But then I added a follow-up recommendation for using jakarta libraries - and I provided the jakarta link for the Activation framework, My Jakarta link in the answer is more up-to-date than your Jakarta link in the comment. Am I missing something here? If so, my apologies. @ShierDexterous
E
1

When running with the spring parent, it was the easiest for me to set a consistent version with this maven property:

<properties>
    <jakarta-activation.version>2.0.1</jakarta-activation.version>
</properties>
Estep answered 9/1 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.