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
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.
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:
👉🏾 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.