• 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;
        }
    }
    
  • 相关阅读:
    C++入门经典-例8.5-多重继承
    C++入门经典-例8.3-子类显示调用父类构造函数
    C++入门经典-例8.2-构造函数的访问顺序
    C++入门经典-类成员的可访问性,继承后的可访问性
    C++入门经典-例8.1-类的继承
    C++入门经典-例7.10-运算符的重载,重载加号运算符
    C++入门经典-例7.9-对象数组,批量化生产
    C++入门经典-例7.8-const对象,标准尺寸
    C++入门经典-例7.7-对象与复制,菌类的繁殖
    C++入门经典-例7.6-this指针,同一个类的不同对象数据
  • 原文地址:https://www.cnblogs.com/jyx140521/p/6867830.html
Copyright © 2020-2023  润新知