2013-06-19 48 views
1

我不知道爲什麼,但是當我嘗試調試,我覺得這是非常奇怪:如果條件得到滿足,但跳到下一個IF

enter image description here

正如你在圖片中看到,in.readLine()的值是nullin.readLine() == nulltrue。但爲什麼它跳過if (in.readLine() == null) { ...線?但是,當我嘗試將斷點放在行266267中時,它將在該條件下輸入代碼。

enter image description here

代碼:

private void startSOfficeService() throws InterruptedException, IOException { 
    if (System.getProperty("os.name").matches(("(?i).*Windows.*"))) { 
     try { 
      //Check if the soffice process is running 
      Process process = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq soffice.exe\""); 
      //Need to wait for this command to execute 
      int code = process.waitFor(); 

      //If we get anything back from readLine, then we know the process is running 
      BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      if (in.readLine() == null) { 
       //Nothing back, then we should execute the process 
       String[] SOFFICE_CMD = { SOFFICE_SERVICE_PATH, 
             "-accept=socket,host=" + SOFFICE_SERVICE_HOST + ",port=" + SOFFICE_SERVICE_PORT + ";urp;", 
             "-invisible", 
             "-nologo"}; 
       process = Runtime.getRuntime().exec(SOFFICE_CMD); 
       code = process.waitFor(); 
       System.out.println("soffice script started"); 
      } else { 
       System.out.println("soffice script is already running"); 
      } 

      in.close(); 
      in = null; 
      System.gc(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

回答

7

當你的調試器將計算in.readLine(),它從讀卡器消耗。因此,如果你正在讀取最後一行,in.readLine()將是非空的,將控制權放在else中,但是當你評估in.readLine()以顯示在調試器中時,它再次讀取,發現沒有更多行,並返回null作爲調試器中顯示的值。

要查看真實圖片,請首先將in.readLine()指定給變量,然後觀察該變量的值,該變量不會通過簡單讀取而改變。

+0

感謝它的工作! :) –

相關問題