1

我不知道为什么,但是当我尝试调试时,我发现这很奇怪:

在此处输入图像描述

如图所示,in.readLine()isnullin.readLine() == nullis的值true。但为什么它会跳过这if (in.readLine() == null) { ...条线?但是当我试图将断点放在行中时266267它会在该条件下输入代码。

在此处输入图像描述

编码:

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();
        }
    }
}
4

1 回答 1

6

当您的调试器评估in.readLine()时,它会从阅读器中消耗。因此,如果您在正在读取的任何内容的最后一行,则将是非空in.readLine()的,将控制权放在null 作为在调试器中显示的值。elsein.readLine()

要查看真实情况,in.readLine()请先分配给一个变量,然后观察该变量的值,仅读取它不会改变它。

于 2013-06-19T04:16:29.277 回答