Make the console wait for user input to close

I have a console application that, after completing its tasks, should give feedback to the user, for example, “operation completed” or “operation failed” and a detailed error.

The fact is that if I just “let it work,” the output message will be printed, but the console will close soon, leaving no time to read the message.

As far as I remember, in C ++, each console application ends with pressing any key to exit or something like that. In C #, I can model this behavior with

Console.ReadKey(); 

But how can I do this in Java? I use the Scanner class, but considering that the “input” is my Scanner instance:

 input.next() System.exit(0); 

Any key will work except return, which is very important here. Any pointers?

+61
java console-application
May 17 '11 at 14:13
source share
8 answers

In Java, it will be System.in.read()

+76
May 17 '11 at 14:22
source share

I would like to add that usually you want the program to wait only if it is connected to the console. Otherwise (for example, if it is part of the pipeline ), it makes no sense to print a message or wait. For this, you can use the Java Console as follows:

 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(); } } 
+18
Mar 15 '13 at 18:49
source share

The problem with entering the Java console is that it buffers the input and requires the enter key to continue.

There are two discussions: Detecting and operating keyboard keys in Java and Parsing a keyboard on a Java keyboard in a console application

The last one used JLine to solve the problem.

I personally have not used it.

+6
May 17 '11 at 14:29
source share

I added what x4u said. Eclipse wanted a try catch block around it, so I let it generate it for me.

 try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

It may have all kinds of bells and whistles, but I think that for beginners who want the command window to not stop, it should be fine.

Also I do not know how widespread this is (this is the first time I make jar files), but it did not start by itself, only through the bat file.

 java.exe -jar mylibrary.jar 

The above was that the bat file was in the same folder. There seems to be a problem with the installation.

Eclipse tutorial appeared: http://eclipsetutorial.sourceforge.net/index.html

Some of the answers also came from: Oracle Thread

+5
Nov 04
source share

I used a simple hack asking windows to use cmd commands and send it to null.

 // Class for Different hacks for better CMD Display import java.io.IOException; public class CMDWindowEffets { public static void getch() throws IOException, InterruptedException { new ProcessBuilder("cmd", "/c", "pause > null").inheritIO().start().waitFor(); } } 
+3
Jun 12 '16 at 16:54 on
source share

You can simply use nextLine(); like a pause

 import.java.util.Scanner // // Scanner scan=new Scanner(); void Read() { System.out.print("Press any key to continue . . . ); scan.nextLine(); } 

However, any button that you press besides Enter, you must press Enter after that, but I found it better than scan.next();

+2
May 20 '15 at 8:37
source share
  public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Press enter to continue....."); s.nextLine(); } 

This nextline pretty good option, as it will help us launch the next line when pressing the enter key

+2
Jun 18 '16 at 4:54 on
source share

A simple trick:

import java.util.Scanner;

/ * Add these codes at the end of your method ... * /

Scanner input = new scanner (System.in);

System.out.print ("Press Enter to exit ...");

input.nextLine ();

0
Jul 18 '18 at 3:25
source share



All Articles