如何处理java system.console()返回null [英] how to handle java system.console() return null

查看:296
本文介绍了如何处理java system.console()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符中获取控制台字符的输入。
例如,有一个字符串 input。如果当前用户输入为 W,则输入为 W,因为用户继续输入 h,输入为 Wh,继续输入 a为 a。
我使用system.console()接受输入,但是在eclipse中,它总是返回null。

I would like to get input from the console character from character. So for example, there's a String "input". If the current user input is "W", input is "W", as the user continues type in "h", input is "Wh" and continues "a" input is "a". I used system.console() to accept the inputs but in eclipse it always return null.

有什么方法可以修复它,或者有另一种方法可以

Is there any way to fix it or there's another way to handle the character by character read from input?

谢谢!

推荐答案

您需要注意以下事项:System.console()方法可以返回null。根据 API

There is something you need to watch out for : The System.console() method can return null. According to the API:

如果虚拟机是从交互式命令行启动的,而没有
重定向标准输入和输出流,则其控制台将存在并且通常会连接到键盘并显示启动虚拟机的位置。如果虚拟机是自动启动的(例如由后台作业调度程序启动),则它通常没有控制台。

尝试从以下位置运行应用程序:控制台/终端/ cmd。

Try to run your application from a console/terminal/cmd.

这篇关于如何处理java system.console()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆