Windows平台上,Java Process执行adb pull卡住的问题

最近用java写了一个小工具,其中有一个操作是从android手机中pull文件夹到电脑上。在mac上调试一切正常,拿到windows上运行时发现会很大概率出现Process卡住的问题,原因不明。

后来将pull文件夹改为pull文件夹中的一个个文件后,不再出现卡住的问题。

卡住的代码

private static void pullFiles(String remoteDir, String localDir) {
    Utils.execute(String.format("%s pull %s %s", Utils.getAdbPath(), remoteDir, localDir));
}

运行正常的代码

private static void pullFiles(String remoteDir, String localDir) {
    List<String> fileNameList = Utils.getExecutionResult(String.format("%s shell ls %s", Utils.getAdbPath(), remoteDir));
    for (String fileName : fileNameList) {
        if (fileName != null) {
            String filePath = dir + "/" + fileName;
            Utils.execute(String.format("%s pull %s %s", Utils.getAdbPath(), file, new File(localDir, fileName).getAbsolutePath()));
        }
    }
}

工具代码

public static List<String> getExecutionResult(String cmd) {
    LOGGER.debug("get execution result cmd : {}", cmd);
    ProcessBuilder builder = new ProcessBuilder();
    if (SystemUtils.IS_OS_WINDOWS) {
        builder.command("cmd", "/c", cmd);
    } else {
        builder.command("sh", "-c", cmd);
    }
    List<String> result = new LinkedList<>();
    Thread t = new Thread(() -> {
        BufferedReader outReader = null;
        try {
            Process p = builder.start();
            outReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = outReader.readLine()) != null) {
                LOGGER.debug("readline : {}", line);
                result.add(line);
            }
            p.waitFor();
        } catch (Exception e) {
            LOGGER.error("occur exception", e);
        } finally {
            if (outReader != null) {
                try {
                    outReader.close();
                } catch (IOException e) {}
            }
        }
    });
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {}
    return result;
}

public static boolean execute(String cmd) {
    LOGGER.debug("execute cmd : {}", cmd);
    ProcessBuilder builder = new ProcessBuilder();
    if (SystemUtils.IS_OS_WINDOWS) {
        builder.command("cmd", "/c", cmd);
    } else {
        builder.command("sh", "-c", cmd);
    }
    try {
        Process p = builder.start();
        p.waitFor();
        return true;
    } catch (Exception e) {
        LOGGER.error("occur exception", e);
        return false;
    }
}