zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    
  • 相关阅读:
    设计一个洗牌的程序?就是将这副牌进行随机交换
    STL中vector,Map,Set的实现原理
    实现一个Memcpy函数:将源指针所指的区域从起始地址开始的n个字节复制到目的指针所指区域
    四个名词(很常见):虚拟内存,虚拟内存地址(线性地址),物理内存,物理内存地址,逻辑地址
    进程的状态
    ubuntu VNC中Xfce4中Tab键失效的解决方法
    GPU安装
    Parted 手册
    opesntack 底层共享存储 迁移配置
    mysql主从同步
  • 原文地址:https://www.cnblogs.com/jyx140521/p/6867830.html
Copyright © 2011-2022 走看看