呓语呢喃

游荡在空气中

JDK6中的System.console()的一个小问题

开始看《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)
不知道什么原因

上网搜了下也没有找到原因,很郁闷.

下午不郁闷了以后继续查找问题,最终解决

根据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.”

基本意思就是当java程序是从命令行中运行的时候,并且标准输入输出流没有被重定向过的话,console是存在的。当vm是自动运行的(应该是包含从ide运行这种情况,system.console()返回的就是null)

posted on 2007-11-07 14:37 Michael Zheng 阅读(1452) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

统计

常用链接

留言簿(1)

随笔分类(4)

随笔档案(36)

搜索

最新评论

阅读排行榜

评论排行榜