Do I have to explicitly call System.exit() in a Webstart application?

Recently I converted a Swing application to Webstart. The process was pretty straightforward, but I found that after I close all windows, my application's JVM did not terminate. The thread dump showed that there are a couple of non-daemon threads, notably Swing's EDT, AWT and a couple of websart related threads.

The actual strategy used is that each window increments a counter when it is created and decrements one when it is closed. The default close operation is DISPOSE_ON_CLOSE. Wen the counter reaches zero, I stop all threadpools and release all JNI resources.

When I launched the application from a bat file (same JARs), it terminated fine when all windows were closed, so I figured that the problem has something to do with Webstart.

Now the questions:

  1. Can anybody tell me what exactly is happening? Why does Webstart leave zombie JVMs?
  2. Is there a way to release the Webstart resources explicitly without halting the JVM?
  3. I've always had the opinion that calling System.exit() encourages the sloppy practice of not releasing your resources and relying on the OS to clean up after you (which can lead to nasty surprises if you reuse the code later)... am I missing something?

See also the followup question for detecting whether the app has been launched by Webstart.


Asked by: David931 | Posted: 21-01-2022






Answer 1

Because of bugs in WebStart, yes. WebStart starts up a "secure thread" for it's own purposes that interacts with the EDT. This SecureThread prevents the automatic termination of the Java process one would expect when all windows and AWT resources are disposed.

For more information see http://www.pushing-pixels.org/?p=232

Answered by: Chester507 | Posted: 22-02-2022



Answer 2

The AWT EDT is usually the culprit. For some years now it has had some logic to shutdown when there are no undisposed windows. However, there are recurrent problems with leaks, including within the implementation of AWT and Swing. Therefore, I strongly suggest using System.exit in production releases (you might possibly want to leave it out for some testing to detect leaks).

The WebStart thread should all be daemon when there are no system windows (console, javax.jnlp services and other dialogs) showing.

Answered by: Briony268 | Posted: 22-02-2022



Answer 3

Webstart starts the Console window (you may be able to disable that). The console window is used to see stdout/err of the webstart process as well as rudimentary log/debug but has the side effect of created a top-level AWT/Swing window. Since the AWT/EDT only ends when the LAST window is disposed, the console window is holding up your application. You should probably call System.exit() to be 100% sure your application exits (unless you can gurantee a certain client configuration, webstart console turned off)

Answered by: Leonardo625 | Posted: 22-02-2022



Answer 4

Consider attaching with jconsole and get a look at what the JVM is doing.

Answered by: Lenny757 | Posted: 22-02-2022



Answer 5

I experience the same issue with web start. If i turn off java console, the process does not hang up. Any known bug id from Sun?

Answered by: Owen413 | Posted: 22-02-2022



Similar questions

Is there a way to terminate a java application that uses java3d, without calling System.exit()?

Java3D starts several system threads and doesn't set the isDaemon flag on them. When I dispose the (only) JFrame of my application it won't terminate because these threads are still running. Calling System.exit() seems to be the only way to terminate the application. (Or killing it from outside, of course). As I don't like to call System.exit() I have tried the following (but without success):


java - Eclipse RCP application is not setting the ERRORLEVEL upon System.exit()

When trying to invoke an Eclipse RCP application from a bat script, If the Eclipse terminates abnormally, then we are exiting the RCP application as System.exit(10). And when I try to do echo %ERRORLEVEL% from script, then it is displaying the 0 instead of 10. Whereas if I do the same thing with an normal Java Application (say with help of a JAR file), then it is logging as 10. An...


java - Calling System.exit() in Servlet's destroy() method

This is a follow up to my earlier question. Tomcat 5.0.28 had a bug where the Servlet's destroy() method was not being invoked by the container on a shutdown. This is fixed in Tomcat 5.0.30, but if the Servlet's destroy() method had a System.exit(), it would result in the Tomcat ...


java - Ignore System.exit() from other class

Given the class below, public class ClassOne { public static void main(String... args) { System.exit(1); } } The following class will be destroyed as well, assuming there are other things to do after ClassOne.main is invoked. public class ClassTwo { public static void main(String... args) { ClassOne.main(args); Thread.sleep(30000); } ...


What can cause Java to keep running after System.exit()?

I have a Java program which is being started via ProcessBuilder from another Java program. System.exit(0) is called from the child program, but for some of our users (on Windows) the java.exe process associated with the child doesn't terminate. The child program has no shutdown hooks, nor does it have a SecurityManager which might stop System.exit() from ter...


java - Graceful way to exit a dm_job ? System.exit()?

If I want to properly exit a documentum java job (if params are invalid for example), should I use a system.exit() or is there another way to do it. As far as I know system.exit closes the virtual machine, does it have an effect on other jobs running?


java - Preventing System.exit() from API

I am using a third party library that does a System.exit() if it encounters exceptions. I am using the APIs from a jar. Is there anyway that I can prevent the System.exit() call because it causes my application to shutdown? I cannot decompile and recompile the jar after removing the System.exit() because of a lot of other licensing issues. I once came across an answer [to some other q...


java - Preventing JVM from executing System.exit()

how to prevent from executing System.exit(). If in middle of code exit() method is called. JVM should throw compile time exception


java - What happens when a thread calls System.exit()?

What exactly occurs? Does the thread stop, or does the program stop? How can I stop the main thread then?


swing - Java: System.exit() parameters

I am having a simple question, but somewhat questionable. This is the situation. In my application, you can open new windows by clicking the new button. When you click the "X" (close) button, it will first ask whether you want to save your work. If no, it will use system.exit(0) to exit. The case is, this closes all the open "new" windows. I want to close only the window which user selected the option "no save". Ho...


java - System.exit() results unexecutable finally block

This question already has answers here:


java - Can you call System.exit() in an exception class

I have some code which calls a custom exception. However, when this exception is reached it causes my program to hang. What I want is when this exception occurs is for my program to stop running completely as this is an unrecoverable position. So my question is: is it bad practice to call System.exit(1) in the constructor of my exception class? or should I instead call it from the code which throws the exception?






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top