Eclipse中无法使用java.io.Console

今天在Eclipse中写一个小程序,用到了Console,结果在运行时报空指针,但是在命令行里面运行却正常。

package g2w.playground.console;

import java.io.Console;

public class ConsoleLogin {
	private static String USERNAME = "greatghoul";
	private static String PASSWORD = "mypass";
	
	public static void main(String[] args) {
		Console console = System.console();
	
		String username = console.readLine("username: ");
		String password = new String(console.readPassword("password: "));
		
		if (USERNAME.equals(username) && PASSWORD.equals(password)) {
			console.printf("Hi, there! Dear %s.", username);
		} else {
			console.printf("Invalid username or password.");
		}
	}

}

 

Java API 中 写道
Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

 

看来是用不了,得用Scanner才行。

 

via: http://www.codeguru.com/forum/showthread.php?t=487190

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值