views:

427

answers:

5

I have an executable jar file. Once started the only way to stop it is to go to the task manager and end the javaw process. Is there a cleaner way to stop it, say with an UI which a novice user can use?

+1  A: 

Since you control the code, you want to have something in the GUI that will allow for exiting using System.exit. So, if you were using Swing, you could do something like:

JButton b = new JButton("Exit");
b.setActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    System.exit(0);
  }
}
Rob Di Marco
Well the problem with this is that there is no GUI application where I can put a button. I could make another GUI application to stop my jar but this GUI would be working on a different jvm so it might not be able to interface with the other application.
+1  A: 

Another useful technique is to listen for windowClosing, which happens when the user clicks the X button on Windows (and the equivalent on other systems):

addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

I usually put that in the constructor of the class that extends Frame for the application.

If you are using a JFrame, you also need to add this line:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Jonathan
Or you could just do `setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)` and avoid the extra `WindowListener`.
Michael Myers
Well the problem with this is that there is no GUI application where I can put a button. I could make another GUI application to stop my jar but this GUI would be working on a different jvm so it might not be able to interface with the other application.
True, but sometimes you need to do some cleanup that you would not have control over that way.
Jonathan
A: 

If your application does not have a GUI - for example if its a service - then you can use local network access to simulate the standard IPC (InterProcess Communication) mechanisms that the operating system normally uses to start and stop services.

A very popular implementation of that you can find in Apache's Commons Daemon project: http://commons.apache.org/daemon/

Guss
I used launch4J to wrap up my jar into an exe, however I WANT it to work as a service, I tried Java Service Wrapper but it doesn't work for me for some reason. I'll look into your link. But as of now my application is not a service.
Whether or not it is a service, if there is no user interface then the best way to control it is through local network access. Open a listening socket that only accepts connections from the local machine, and wait for commands to arrive in a separate thread.
Jonathan
Commons Daemon already implemented that for you - they provide both the "server" and "client" for that methodology.
Guss
A: 

Your application will end when all non-daemon threads have ended. A way to do this, would be to make all threads but one daemons, and let the last thread be the one that awaits the signal to stop.

Thorbjørn Ravn Andersen
A: 

I finally found the solution to my problem. Since I don't have a GUI which I can close, nor do I have a console. The best way is to use the SystemTray API in the java.awt package. Just right click on the icon and exit.