Java Virtual Machine Doesn’t Exit

You may have been surprised and frustrated to find that in some cases, the Java Virtual Machine (JVM) does not stop running when your application exits the static main() method. For example:

 public class FrameTest extends java.awt.Frame {	public static void main(String[] args) {		FrameTest ft = new FrameTest();		ft.setVisible(true);		ft.setVisible(false);		ft.dispose();		ft = null;	}  //  public static void main()}  //  public class FrameTest extends java.awt.Frame

When this code is run as an application, the JVM will not exit. However, it will exit if you delete the calls to setVisible() and run it again. The reason for this change in behavior is that making the Frame visible activates the event thread, which is responsible for calling event handler methods and for painting. Once the event thread is running, the JVM doesn’t exit because the event thread is not a daemon thread, and the JVM only exits when no daemon threads are running. To force the JVM to exit, you should call the System.exit() method, which takes a single integer value as a parameter. For example:

 public class FrameTest extends java.awt.Frame {	public static void main(String[] args) {		FrameTest ft = new FrameTest();		ft.setVisible(true);		ft.setVisible(false);		ft.dispose();		ft = null;		System.exit(0);	}  //  public static void main()}  //  public class FrameTest extends java.awt.Frame
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS