開始看《Java 6 Platform Revealed》
在MyEclipse里面寫了個簡單的例子,用到了java.io.Console類
public static void main(String[] args) {
String s = "hello";
System.out.println("helloworld");
System.console().printf("%s%n",s);
//System.console().readLine();
}
運行報錯
Exception in thread "main"
java.lang.NullPointerException
at ch.zhengyiyu.emptyString.main(emptyString.java:10)
不知道什么原因
上網(wǎng)搜了下也沒有找到原因,很郁悶.
下午不郁悶了以后繼續(xù)查找問題,最終解決
根據(jù)spec的描述
“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.”
基本意思就是當(dāng)java程序是從命令行中運行的時候,并且標(biāo)準(zhǔn)輸入輸出流沒有被重定向過的話,console是存在的。當(dāng)vm是自動運行的(應(yīng)該是包含從ide運行這種情況,system.console()返回的就是null)