让控制台等待用户输入关闭

我有一个控制台应用,在执行其任务后,必须向用户提供反馈,例如“操作完成”或“操作失败”以及详细的错误。

问题是,如果我只是“让它运行”,输出消息将被打印出来,但控制台不久之后就会关闭,没有时间读取消息。

据我所知,在 c + + 中,每个控制台应用都会以“按任意键退出”之类的结尾。在 C # 中,我可以使用

Console.ReadKey();

但是用 Java 怎么做呢?我正在使用 Scanner 类,但是考虑到“ input”是我的 Scanner 实例:

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

“任何钥匙”都行,除了回车,这可是个大问题,有什么建议吗?

252440 次浏览

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

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.

I've put in 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 can probably have all sorts of bells and whistles on it but I think for beginners that want a command line window not quitting this should be fine.

Also I don't know how common this is (this is my first time making jar files), but it wouldn't run by itself, only via a bat file.

java.exe -jar mylibrary.jar

The above is what the bat file had in the same folder. Seems to be an install issue.

Eclipse tutorial came from: http://eclipsetutorial.sourceforge.net/index.html

Some of the answer also came from: Oracle Thread

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();
}
}

You can just use nextLine(); as pause

import java.util.Scanner
//
//
Scanner scan = new Scanner(System.in);


void Read()
{
System.out.print("Press any key to continue . . . ");
scan.nextLine();
}

However any button you press except Enter means you will have to press Enter after that but I found it better than scan.next();

I used 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();
}
}
public static void main(String args[])
{
Scanner s = new Scanner(System.in);


System.out.println("Press enter to continue.....");


s.nextLine();
}

This nextline is a pretty good option as it will help us run next line whenever the enter key is pressed.

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 quit...");
input.nextLine();