How to stop a Java application?

I made a java application and packaged it into a jar executable. Now the user can run this program in one of the following two ways:

  • Run if from a command prompt by running the following command at a command prompt:

    java -jar "MyJar.jar"

  • Double click on this jar file.

I want my client to take the second approach, because it is much simpler than the first approach. But the problem with the second approach is how to stop the application before it is completed?

This is a command line application.

And the command line window does not appear when the user double-clicks on the jar file. So, does Ctrl + C work in this case?

+4
source share
10 answers

The stop (exit) of the application must be inside the application. Regardless of whether it is a command line or a graphical interface, the application developer must write code to exit it (for example, in a command-line application you can have something like Press 5 to exit , Press Esc to Exit , etc. .) And in a GUI application, you will need to write the code to exit when the window is closed, or the EXIT button (or others, depending on your application)

Ctrl + C is a KILL application. This is not a normal way out. For GUI applications, the user usually (on Windows) goes to the task manager and ends the process (similar methods on other operating systems)

But these are abnormal exits - when the user wants to kill the application, when, for example, the application no longer responds. Normal outputs should be provided by the application (and therefore by the programmer - you)

+9
source

I had a similar problem. I have some Java programs that are basically long-running daemon processes. It's nice to be able to stop them and bounce (restart).

I used two approaches. Both have advantages and disadvantages. One of them is to configure the signal handler by placing such a function in some class of your program (in my case, it is in the class with the main method).

 import sun.misc.Signal; import sun.misc.SignalHandler; ... private static boolean stopNow = false; private static boolean restartNow = false; ... private static void handleSignals() { try { Signal.handle(new Signal("TERM"), new SignalHandler() { // Signal handler method for CTRL-C and simple kill command. public void handle(Signal signal) { MyClass.stopNow = true; } }); } catch (final IllegalArgumentException e) { logger.warn("No SIGTERM handling in this instance."); } try { Signal.handle(new Signal("INT"), new SignalHandler() { // Signal handler method for kill -INT command public void handle(Signal signal) { MyClass.stopNow = true; } }); } catch (final IllegalArgumentException e) { logger.debug("No SIGINT handling in this instance."); } try { Signal.handle(new Signal("HUP"), new SignalHandler() { // Signal handler method for kill -HUP command public void handle(Signal signal) { MyClass.restartNow = true; } }); } catch (final IllegalArgumentException e) { logger.warn("No SIGHUP handling in this instance."); } } 

It works solidly for us in production. For this you need a real Sun JRE; the one that comes with a typical Linux distribution does not contain a signal. It also works fine on Windows, but you are not getting a HUP signal. To run this thing you need a shortcut or shellscript.

Also, keep in mind that signal processing is a big bold notch. Do not try to do too much inside your signal handler. You can see that my sample code just sets static flags. Other parts of my program find that the flag is changed and turned off. I could experiment with more complex code inside the signal handler, but I did not want to take the burden of QA.

Another approach is to structure your program as a servlet. You will write a class that extends the HttpServlet in this case. Override Servlet.init with a method that starts your workflow. Similarly, override Servlet.destroy with a method that disables.

You can then use a Java EE container container, such as Tomcat, to control starting and stopping.

+5
source

If your program is a console mode program, and it concludes, Ctrl-C should be able to kill it.

If this is a GUI program, you want to give it a button to exit, or at least set EXIT_ON_CLOSE as the defaultCloseOperation your main JFrame.

+2
source

ctrl + alt + suppr → kill javaw.exe ?: P

Or you will need to present a user interface with a stop button (see swing, etc.)

+1
source

It depends on the user interface. If this is a Swing application, you can set DefaulCloseOperation (JFrame.EXIT_ON_CLOSE) to your main frame. If his console application and the user interact with him, you will ask the user to enter a value indicating that they want to stop the application. If yoy doesn't interact with the user at all, ctrl-c will work.

+1
source

Is this a graphical application or a command line.

In the first case, just handle the window close event. In the second case, the handle, for example, CTRL + C

0
source

What depends on which application it is?

Is this a rocking app? If so, then your application should process when the user clicks the close button. There is a behavior for this. JFrame.close()

If this is not a swing application, then ctrl + c will work.

0
source

"This is a command line application." You can do this so that when the user clicks a button (e.g. esc), he can write short commands to exit, restart, etc.

You can do this with KeyListener . When the ESC is hit (say that this is the button you want the user to hit), you use Scanner on System.in and you will do System.exit (0); if the input is "output".

0
source

I used a socket connection to enable kill running running.

  //send kill signal to running instance, if any try { new Socket("localhost", 4000).getInputStream().read(); //block until its done } catch (Exception e) { //if no one is listening, we're the only instance } //start kill listener for self new Thread() { @Override public void run() { try { ServerSocket serverSocket = new ServerSocket(4000); serverSocket.accept(); //do cleanup here serverSocket.close(); } catch (Exception e) { } System.exit(0); } }.start(); 
0
source

You can use this: exit ()

You can predefine all actions before the virtual machine stops completely, so you can save your data and perform all actions to prevent data loss. I'm not sure if this is a good way because I just started learning Java, but it seems to work.

0
source

All Articles