Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many ways a java Program can end?

Tags:

java

I know use System.exit(0) can end a java program, for instance, if I have a JFrame window, it will close and end the program, but I wonder how many other ways, can it be closed and the program be ended ? Including when an error occurs, will the program be shut down and the JFrame be closed ?

like image 649
Frank Avatar asked Feb 04 '10 18:02

Frank


People also ask

How do you end a Java program?

To end a Java program, we can use the exit() method of the System class. It is the most popular way to end a program in Java. System. exit() terminates the Java Virtual Machine(JVM) that exits the current program that we are running.

How do you stop an infinite loop in Java?

Just type break; after the statement after which you want to break the loop.

Which method will the program end immediately?

We used functions like exit() in C++ to abnormally terminate the execution of program, which function can we use in Java.

What is used to terminate all Java commands?

exit() method exits current program by terminating running Java virtual machine. This method takes a status code.


2 Answers

To add to other answers:

  • If the process that is hosting the VM is forcefully terminated, your program will spontaneously disappear
  • The same happens if the plug gets pulled on the machine hosting the VM :)
like image 146
Merlyn Morgan-Graham Avatar answered Sep 22 '22 17:09

Merlyn Morgan-Graham


A Java program ends when the last Thread without daemon flag ends, or when you call a method that shuts down the virtual machine (System.exit(), Runtime.exit(), Runtime.halt() and possibly a few more).

Anything else is up to libraries that call System.exit() (such as a JFrame with EXIT_ON_CLOSE).

like image 35
Tim Jansen Avatar answered Sep 24 '22 17:09

Tim Jansen