Search code examples
javaconsoleexit

How to auto close the console in BlueJ


I want to exit only the console in a method.

I tried this:

import java.util.Scanner;

Scanner input = new Scanner(System.in);
System.out.print("Press Enter to quit...");
input.nextLine();

and now I want only to end the console, I tried System.exit(0);, but this only "deletes" the object and the console stays. Is there any other command I can use?

I'm using BlueJ for a school project and there we have the console that appears with System.out.println(), this console I want to close... I know you can click on the X of the console window, but I want to do it automatically in the method when you click enter for example


Solution

  • tl;dr

    No, your Java app cannot control the outer shell app running in a console. Indeed, it makes no sense for your Java app to want to manipulate or kill that console.

    Details

    The console, where text appears and where the user types, is always under the control of another app, not your Java app.

    To invoke a non-GUI Java app, you need to already be using a console. That console exists before your Java app runs. And that console exists after your Java app runs. Indeed, the console reports the ultimate result of your Java app — like any console app, your Java app reports an exit code, an integer number generated as it exits. The exit code indicate whether the app ran successfully or not. For example:

    Process finished with exit code 0

    The “outer” app providing the console can be:

    That last describes your situation in BlueJ. BlueJ is a specialized IDE that hides much of the elaborate ceremony involved in Java programming.

    But the basic idea is the same: When you run your Java app, BlueJ constructs a console within its own application windowing, runs some kind of shell within that console, and executes your Java app. BlueJ is designed to hide the nitty-gritty details from you as a beginning student, but the actions underneath are essentially the same.

    You can think of a computer as concentric circles:

    • In the core middle is the CPU.
    • A ring outside that is the operating system.
    • A further ring is a console app to interface with a human.
    • Next ring out is a shell app run by that console, such as zsh, bash, etc. The shell provides for convenient interactions with the human through a command-line interface. A shell is actually an app within the console; the human user can exit one shell and start another such as switching from bash to zsh.
    • Another ring outside that is any app invoked on the command-line interface of the shell. For example, the java executable (“command”) to run a JVM, inside of which your Java code executes. Eventually your Java app ends, and the java command exits, returning an exit code to the shell app.
    • Optionally, a GUI can launch such as seen on a Mac, MS Windows, or Linux/BSD desktop computer. You can think of this as yet another outer ring. You can see GUI computers running the console during boot-up, showing console activity either by default or by alternate booting scenario.

    👉🏾 The point is: The console & shell app exist before, during, and after your Java app execution.

    In your case, BlueJ is constructing that console & shell, and maintaining it before and after your Java app runs. So your Java app has no control over the creation/destruction of the console & shell.


    By the way, the Java team at Oracle, and the Java community, have been working on providing easier on-ramps for people new to Java.