How to exit(close to be specific) the terminal window through Java in Linux after the termination of the Java Program

I've made a project in Java and I run its .jar file on the Linux terminal but after the successful termination of the program my program ends but the Terminal window did not close. I tried "java.lang.System.exit(0)", but then also the window did not close.

I'm running program by opening terminal and running java -jar filename.jar

I would like the terminal to exit when the Java program exits.

3

1 Answer

As you already mention, System.exit() just exits the Java program, not the invoking shell. There are two ways to close the terminal window after the Java program has stopped:

Either run the Java program and add the exit command in one line:

java -jar filename.jar; exit

This will invoke java -jar filename.jar and when that command returns, the shell's exit command is executed (effectively closing the terminal window).

Or replace the current shell with the Java process by issuing

exec java -jar filename.jar

This way the current instance of bash is replaced with an instance ofjava. Hence, when the Java process stops, the terminal window will close.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like