2013-06-19 50 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

感谢它的工作! :) –

相关问题