JavaHow to Use the halt() Method in Java

How to Use the halt() Method in Java

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This Java quick tip teaches developers how to use the halt() method of the Runtime class to forcibly terminate the currently running Java Virtual Machine (JVM). Enter the following code into your code editor or integrated development environment (IDE):

*/

public class RuntimeHalt
{
	public static void main(String args[])
	{
		RuntimeHalt runtimeHalt = new RuntimeHalt();
		runtimeHalt.proceed();
	}
	
	private void proceed()
	{
		System.out.println("Before Runtime.getRuntime().halt()");
		//Invoking halt on the Runtime, terminates the currently running JVM forcibly.
		Runtime.getRuntime().halt(0);
		//The below text is never printed.
		System.out.println("After Runtime.getRuntime().halt()");
		
	}
}

/*

When you run this code, you should get a similar output to the following. Note: your output may vary on depending upon your operating system, configuration, and settings.

[root@mypc]# java RuntimeHalt
Before Runtime.getRuntime().halt()

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories