Make the console wait for a user input to close Make the console wait for a user input to close java java

Make the console wait for a user input to close


In Java this would be System.in.read()


I'd like to add that usually you'll want the program to wait only if it's connected to a console. Otherwise (like if it's a part of a pipeline) there is no point printing a message or waiting. For that you could use Java's Console like this:

import java.io.Console;// ...public static void waitForEnter(String message, Object... args) {    Console c = System.console();    if (c != null) {        // printf-like arguments        if (message != null)            c.format(message, args);        c.format("\nPress ENTER to proceed.\n");        c.readLine();    }}


The problem with Java console input is that it's buffered input, and requires an enter key to continue.

There are these two discussions:Detecting and acting on keyboard direction keys in Java and Java keyboard input parsing in a console app

The latter of which used JLine to get his problem solved.

I personally haven't used it.