JavaFX: NoClassDefFoundError: jakarta/mail/internet/AddressException when creating Jar

46 Views Asked by At

I am trying to add the ability to send emails via my javafx application through the Jakarta mail dependency. It runs perfectly fine in the IDE and I am able to send emails while testing but once I build my artifacts to an executable jar and attempt to run the executable jar, I get the following error stack:

Caused by: java.lang.NoClassDefFoundError: jakarta/mail/internet/AddressException
        at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3429)
        at java.base/java.lang.Class.getConstructor0(Class.java:3634)
        at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2810)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:939)
        at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:981)
        at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:755)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2845)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2641)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2523)
        at com.Sapplication.Controllers.DashControllerAdmin.initPanes(DashControllerAdmin.java:114)
        at com.Sapplication.Controllers.DashControllerAdmin.initialize(DashControllerAdmin.java:77)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2662)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2523)
        at com.Sapplication.StageControllers.CreateStage.createStage(CreateStage.java:23)
        at com.Sapplication.Controllers.LoginController.btnSignIn(LoginController.java:203)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        ... 36 more
Caused by: java.lang.ClassNotFoundException: jakarta.mail.internet.AddressException
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 56 more

**This is the code in my program: **

    Authenticator authenticator = new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USERNAME, PASSWORD);
        }
    };

    Session session = Session.getInstance(PROPERTIES, authenticator);
    session.setDebug(debug);

    try {

        // create a message with headers
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        // create message body
        Multipart mp = new MimeMultipart();
        for (String message : messages) {
            MimeBodyPart mbp = new MimeBodyPart();
            mbp.setText(message, "us-ascii");
            mp.addBodyPart(mbp);
        }
        msg.setContent(mp);

        // send the message
        Transport.send(msg);
        JOptionPane.showMessageDialog(null,"Email Sent Successfully!");
    } catch (AddressException e) {
        throw new RuntimeException(e);
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}`

If I comment out the code and rebuild my artifacts the program runs just fine!

I have gone through multiple forums of people having the same issue and have followed a few suggestions 1.) I added both the Jakarta mail dependency and the Jakarta activation dependency to my POM

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

2.) I've also gone into Project structure -> Libraries & Project structure -> Modules and made sure that both Maven: com.sun.activation:jakarta.activation:2.0.1 and Maven: com.sun.mail:jakarta.mail:2.0.1 are showing up. I've also tried deleting the maven version and downloading the jars and adding the jars directly as well as keeping the maven version and installing the jars but both ways are still causing the same error upon trying to run the program after building the jar.

3.) A few answers said to add it to my VM options and These are my current VM options:

--module-path

"C:\Program Files\Java\javafx-sdk-19.0.2.1\lib"

--add-modules

javafx.controls,javafx.fxml,jakarta.activation,jakarta.mail

--add-exports

javafx.base/com.sun.javafx.event=ALL-UNNAMED

A few more details:

IDE: IntellJ SDK: OpenJDK 19.0.2

If anyone has any idea why I'm still getting the error, I would really appreciate the help thank you! (Sorry for any formatting issues, this is my first time asking a question on here!)

Edit: Command in CMD:

java -jar "C:\Users\Main PC\Desktop\SApplication.jar"

I am able to run the application without the jakarta.mail dependency fine using this method!

To create the excutable jar: I go to Build -> build artifacts -> builds to an executable jar in the out folder of the project. I use the jar with jdeploy later to manage updates.

Edit: Modules in the project. Here is the modules in the project

0

There are 0 best solutions below