Exit java applications properly (Java article)
We submitted our java tool Java
Launcher to download.com and got
following user opinion there:
Excellent utility one major flaw!
This program makes running Java class files a breeze; I love the windows
executable option as well. You can also have custom icons. The only major flaw
I found is with the windows .exe files. Once you run a program this way and
close it, it keeps running in the background. This eats up memory and prevents
you from moving the file etc. Otherwise a real nifty tool!
24-Jan-2005 05:50:02 PM
We appreciate his opinion, but this flaw is caused by user's
programming errors, not by Java
That is, user's java applications do not exit from their running processes
We hope this short article can help for solving this kind programming problems.
Exiting from Java running process is very easy, basically you need to do just
two simple things:
- call java method System.exit(...) at application's quit point.
For example, if your application is frame based, you can add listener WindowAdapter
and and call System.exit(...) inside its method windowClosing(WindowEvent e).
Note: you must call System.exit(...) otherwise your program is
- Avoiding unexpected java exceptions to make sure the exit method can be
If you add System.exit(...) at right point, It does not mean that the
method can be called always, because unexpected java exceptions may prevent
the method from been called: this is strongly related to your programming skills.
Following is a simplest sample (JFrame based) which shows you
how to call exit method
public class ExitApp extends JFrame
public void windowClosing(WindowEvent e)
call the method
public static void main(String sCommand)
ExitApp app=new ExitApp();
It is unbelievable that many java samples from authorized organizations do not
call the exit method, in other words, all of those samples contain basic
You may ask: "My applications do not call this method, but it still can
exit properly, why?"
Your question is not correct because your applications don't exit properly
except they are very simple: they never create any threads and interfaces (do
nothing) - simpler than sample above.
If you use a shell interface (i.e. DOS window) to execute an application by
java command java MyApplication, when you close the DOS window, your
application is closed also.
Please realize that the application is not terminated by itself, instead, it
is terminated by the shell interface - the shell interface terminates the running
process of your application - similar to operating system terminates
applications' running process when you shut down your computer.
If you use javaw.exe to execute your applications as javaw MyApplication,
you will know you are in trouble at once.
We offer you two advanced java tools
- SyncJEdit (a java IDE)
Hope you enjoy them