1

I am trying to send an email in my JavaFX application Everything works perfect, email is sent to Reciepent, i get no exceptions and no errors when i run it in IDE (InteliJ) but when I run the app outside Intelij it doesn't work. I made simple registration form which saves data from fields into my database:

String username;
String password;
String email;
Random rd = new Random();
int ID;
public void registerUser(javafx.event.ActionEvent ab) {
     username = textUser.getText();
     password = textPass.getText();
     email = textEmail.getText();

    ID = rd.nextInt(999999999);
    Connection connectt = null;


    try {

        Class.forName("org.sqlite.JDBC");
        connectt = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\barte\\OneDrive\\Desktop\\sqlite databases\\PRODUCTS\\Products.db");
        String s = "INSERT INTO Users(Username,Password,Email,UserID)  VALUES (?,?,?,?) ";
        PreparedStatement registera = connectt.prepareStatement(s);
        registera.setString(1, username);
        registera.setString(2, password);
        registera.setString(3, email);
        registera.setInt(4, ID);

        System.out.println(username);
        System.out.println(password);
        System.out.println(email);
        registera.executeUpdate();
        System.out.println("Added to Database");
        sendMail();
        registerr.setStyle("-fx-background-color: #69ff59;");
        registerr.setText("Check Your MailBox");
        registerr.setOnMouseClicked(event -> {
            registerr.setText("Email Has been sent");
        });
        textUser.setText(null);
        textEmail.setText(null);
        textPass.setText(null);
        regiPane.setVisible(false);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

And here is code for sending email:

public void sendMail() throws MessagingException {
    String USER_NAME = "stoc****";
    String from = USER_NAME;
    String PASSWORD = "************";
    String pass = PASSWORD;
    String RECIPT = textEmail.getText();
    String TOPIC = "Welcome " + username + "!";
    String BODY = "Dear user! " +
            "You can sign into StockFX by your ID/Username and password" +
            "User ID: " + ID + "\n" + "Password: " + password + "\n" +
            "We would like to thank you for using our services now and in future!";
    String[] to = {RECIPT};
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(
                    "****", PASSWORD);
        }
    });
    MimeMessage message = new MimeMessage(session);


    try {
        try {
            message.setFrom(new InternetAddress(from));
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        InternetAddress[] toAddress = new InternetAddress[to.length];
        // To get the array of addresses
        for (int i = 0; i < to.length; i++) {
            try {
                toAddress[i] = new InternetAddress(to[i]);
            } catch (AddressException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < toAddress.length; i++) {
            try {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        try {
            message.setSubject(TOPIC);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        try {
            message.setText(BODY);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        try {
            message.saveChanges();
        } catch (MessagingException e) {
            e.printStackTrace();
        }


        registerr.setStyle("-fx-background-color: #69ff59;");
        registerr.setText("You can now log in");
        registerr.setDisable(false);
        textUser.setText(null);
        textEmail.setText(null);
        textPass.setText(null);
        regiPane.setVisible(false);
        Transport transport = session.getTransport("smtp");
        System.out.println("get protocl");
        transport.connect(host, from, pass);
        System.out.println("get host,from and password");
        transport.sendMessage(message, message.getAllRecipients());
        System.out.println("get recipients");
        transport.close();
        System.out.println("close");
        System.out.println("Email Sent Successfully!");
    } finally {
        System.out.println("Complete Process");
    }

}

Everything works fine inside InteliJ but void sendEmail won't work in runable jar I am new to Java mail. Imports:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.*;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;

This class is Controller Class

And this is the main Class:

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;

public class Main extends Application {

@Override
public void start(Stage UI) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("DashBoard.fxml"));
    UI.setTitle("DIREXT SCANNER (DEMO VER 0.5)");
    UI.setScene(new Scene(root, 800, 600));
    UI.initStyle(StageStyle.UNDECORATED);
    UI.setResizable(false);
    UI.show();
    UI.setFullScreenExitHint("Press 'ESC' to exit full screen");

}
public static void main(String[] args){
    launch(args);
}
}

I tried rebuilding project, delete and add libraries again but the same result Did anybody elese encountered the same problem? Is it IDE related or am I missing imports or methods? I tried to look for similar question on forums, I have already fixed few things, as before runable jar wouldn't run at all. If question exist please can someone provide the link.

edit this is the error i get when i run jar from PowerShell:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
        at javafx.graphics/javafx.scene.Node.fireEvent(Unknown Source)
        at javafx.controls/javafx.scene.control.Button.fire(Unknown Source)
        at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
        at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
        at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
        at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
        at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
        ... 52 more
Caused by: java.lang.NoClassDefFoundError: javax/activation/DataHandler
        at sample.DashBoardController.registerUser(DashBoardController.java:321)
        ... 62 more
Caused by: java.lang.ClassNotFoundException: javax.activation.DataHandler
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 63 more
Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Are you getting any errors? – Slaw Sep 13 '18 at 14:58
  • Nope, I get no errors and no exceptions, when i run it in IDE, email sends, email password and username inserts to database, when i check my inbox, email is there, but outside ide, nothing happens, only data is in database. Email is not sent – Bartek Kepke BePositive Sep 13 '18 at 15:00
  • I also tried disable my firewall, and some security settings, with no result.. – Bartek Kepke BePositive Sep 13 '18 at 15:09
  • Just to make sure: You're launching the jar from the command line so you can see any printed stack traces, right? – Slaw Sep 13 '18 at 15:11
  • I launched app in PowerShell and I got this java.lang.NoClassDefFoundError: javax/activation/DataHandler which is the line MimeMessage message = new MimeMessage(session); in sendMail void – Bartek Kepke BePositive Sep 13 '18 at 21:40
  • Why do i get this exception only outside IDE? – Bartek Kepke BePositive Sep 13 '18 at 23:49
  • Some (maybe all) of your application's dependencies are missing when you run your app from the jar. Keep in mind that when launching from your IDE the IDE or build tool takes care of the classpath for you. Once you go to deploy your project as an executable jar you need to make sure the dependencies are included and that they are on the classpath. – Slaw Sep 14 '18 at 04:59
  • These may help: https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java and https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror – Slaw Sep 14 '18 at 05:04
  • See https://stackoverflow.com/a/48750939/2000323. – Andrey Sep 14 '18 at 07:23
  • Thanks I got it solved – Bartek Kepke BePositive Sep 14 '18 at 16:41

2 Answers2

1

I found solution thanks to all your help What i did, in project structure, I deleted all artifact, modules and libraries and add everything back again then rebuild the project then build the artifact and I also did set my project folder as Source Root and it worked.

0

If you are using External Libraries So The problem Sometimes Happens in the Path of this Libraries When you extract to jar File , Simple Solution that Sometimes Work is to create new project and create new Files As the Old Project And move them