TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Terminate a Java Application

Overview

Normally the java program terminates when the main method finishes the execution. Sometimes, we need to terminate the Java application from the code. For example, we may want to terminate the Java application if the user chooses to the Quit menu option, etc. To terminate a Java application, we can use System.exit(statusCode) method.

System.exit(statusCode)

exit() method is a static method in the System class. This method terminates the currently running JVM. We can pass an argument as the status code to this method. A zero status code implies normal termination. A non-zero status code implies abnormal JVM termination.

For example,

System.exit(0) method to terminate a Java application or program gracefully.

This is equivalent to the following line of code:

Runtime.getRuntime().exit(0);

Every Java application has a single instance of the class Runtime. We can interface with the environment using the Runtime instance.

Note that: The Java application cannot create the instance of the class. To get the current runtime we need to rely on and invoke the getRuntime() static method on the Runtime class.

 

public class TerminateJavaApplication {

 public static void main(String[] args) {
 System.out.println("Good Bye!");
 System.exit(0); //Terminate JVM
 System.out.println("Unreachable");
 }
}

 

Terminate Java Application

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Checkstyle Source Code Analyzer Tool› The Javap Utility – Java Class File Disassembler

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com