본문 바로가기

카테고리 없음

Java Swing End Program

Java Swing End Program

Because System.exit is just another method to the compiler. It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits). Your OS or shell can read the integer that is passed back in the System.exit method.

It is standard for 0 to mean 'program quit and everything went OK' and any other value to notify an error occurred. It is up to the developer to document these return values for any users.return on the other hand is a reserved key word that the compiler knows well.return returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any).

In your code above it returns void as you have not supplied anything to return.

In order to close Java program we need to consider which kind of Java application it is?, because termination of Java application varies between normal core java program to swing GUI application. In general all Java program terminates automatically once all user threads created by program finishes its execution, including main thread. Doesn't wait for so as soon as last user thread finished, Java program will terminate. If you want to close or terminate your java application before this your only option is to use System.exit(int status) or Runtime.getRuntime.exit. This cause JVM to abandon all threads and exit immediately.

Shutdown hooks are get called to allow some last minute clearing before JVM actually terminates. System.exit also accept an int status parameter where a non zero value denote abnormal execute and its the result returned by java command to caller. In this java tutorial we will see example of closing both Java program and Java Swing application.

How to exit java program gracefully

This is also a which you can ask to any GUI developer and my second article in swing after writing i. Swing application mostly uses JFrame as top level container which provides two option to close swing GUI application from code. First option which is default is EXITONCLOSE which terminates Java swing GUI program when you click close button on JFrame window. Another option is DISPOSEONCLOSE which terminates if last displayable window is disposed off.

Java Swing Components

Difference between EXITONCLOSE and DISPOSEONCLOSE is that if you have a non daemon thread running it will not be closed in case of DISPOSEONCLOSE, while EXITONCLOSE terminate JVM even if user thread is running. Run the below example by un comment DISPOSEONCLOSE in your and you can see user thread running even after clicking on close button. Here is a complete code example of closing Swing application in Java.