囈語呢喃

          游蕩在空氣中

          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 閱讀(1461) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           

          導航

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

          統計

          常用鏈接

          留言簿(1)

          隨筆分類(4)

          隨筆檔案(36)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 雅安市| 讷河市| 闵行区| 班戈县| 侯马市| 象山县| 漠河县| 梅河口市| 微博| 宝鸡市| 建德市| 克拉玛依市| 锡林郭勒盟| 宣恩县| 盐边县| 禄丰县| 昂仁县| 万州区| 建瓯市| 汶川县| 平远县| 资溪县| 正安县| 英山县| 浦江县| 高清| 普定县| 九龙坡区| 肥东县| 庆城县| 绩溪县| 宜兰县| 江孜县| 新巴尔虎左旗| 瑞安市| 文昌市| 吉隆县| 安达市| 高州市| 泽州县| 集安市|