IF条件已达到但跳过下一个IF

mrj*_*_05 1 java if-statement

我不确定为什么,但在我尝试调试时,我发现这很奇怪:

在此输入图像描述

正如您在图像中看到的那样,in.readLine()is null和in.readLine() == nullis 的值true.但为什么它会跳过这if (in.readLine() == null) { ...条线呢?但是,当我试图把断点的行266和267,它的进入对这一条件的代码.

在此输入图像描述

代码:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Rya*_*art 6

当您的调试器进行评估时in.readLine(),它会从阅读器中消耗掉.因此,如果你在读取的任何内容的最后一行,in.readLine()将是非null,将控制权放入else,但当你评估in.readLine()在调试器中显示时,它再次读取,发现没有更多的行,并返回null作为调试器中显示的值.

要查看实际图片,in.readLine()请先指定一个变量,然后观察该变量的值,只需读取该变量即可.