0

For some reason there are processes in my app that continue even after closing the application's window and exiting. Is there something I haven't accounted for? My app periodically generates a system tray notification, but these continue even after exiting. The only way to stop it is to find the java.exe process in the Task Manager. Is there something I'm overlooking?

public class TrayNotification {

    public static void execute(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
        if (SystemTray.isSupported()) {
            TrayNotification trayNotification = new TrayNotification();
            trayNotification.displayTray(firstName, incidentNumber, requestId, tabTable);
        }
    }

    private void displayTray(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
        // Obtain a single instance of SystemTray
        SystemTray tray = SystemTray.getSystemTray();

        // Set TrayIcon values
        Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/tray-icon.png"));
        final TrayIcon trayIcon = new TrayIcon(image, "Remedy React Notification");
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip(incidentNumber);

        // Add TrayIcon to SystemTray instance if possible
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            // System.out.println("Notification could not be added.");
        }

        trayIcon.addActionListener(e -> {
            Parent root = null;
            try {
                root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml"));
                root.requestLayout();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println("Test");
        });

        // Display TrayIcon 
            trayIcon.displayMessage("Hey, " + firstName, "You are now tracking " + incidentNumber + " !", TrayIcon.MessageType.INFO);

    }

}
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • 1
    Most probable cause is the creation of non-daemon threads. Are you creating any new threads in your program, either explicitly or through a executor? Consider trying to provide an [MCVE]. – Itai Aug 16 '17 at 09:32
  • More [here](https://stackoverflow.com/q/2213340/230513) and [here](http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.8). – trashgod Aug 16 '17 at 09:48

1 Answers1

1

I am using System.exit(0) when the user clicks "Exit" button.

Binyamin Regev
  • 914
  • 5
  • 19
  • 31
  • My problem is I don't have kind of handler that specifies the window's "exit" button. I don't have an Exit menu item and won't be implementing one... I think I need something along the lines of this post: https://stackoverflow.com/questions/26619566/javafx-stage-close-handler – Martin Erlic Aug 16 '17 at 08:58
  • 1
    @santafebound - If you don't have an "Exit" option, how do you define "exiting"? You said "these continue even after exiting" - at the point of "exiting", calling `System.exit(0)` should terminate the JVM instance. – Itai Aug 16 '17 at 09:38
  • Are you using the [X] on the top left/right corner to exit the application? – Binyamin Regev Aug 16 '17 at 10:31