T8Z
T8Z

Reputation: 691

What does the JVM do after finishing an application?

I want to know what JVM do when finish the application. Did it run System.exit() and what is different between System.exit() and SystemExiter()?

Upvotes: 0

Views: 134

Answers (1)

Duncan Jones
Duncan Jones

Reputation: 69399

How exactly the JVM shuts down will differ between implementations. Remember that a JVM is not written in Java and will not call System.exit() to end itself.

SystemExiter is a Spring interface that abstracts calls to System.exit(). Applications can use that interface rather than calling System.exit() directly, allowing the behaviour to be modified during tests. If a class were to call System.exit() during a test, it would end the test cycle because the JVM would shut down. Example:

public class SomeClass {

  private SystemExiter exiter; // inject this or accept in constructor

  public void someMethod() {
    // ...

    exiter.exit(1);
  }
}

In a production system, you could use the JvmSystemExiter implementation to actually shut the VM. In a test, you'd probably mock the SystemExiter interface and check that exit() was called.

Upvotes: 2

Related Questions