zoukankan      html  css  js  c++  java
  • Java执行Linux命令并返回命令结果

    原文地址 https://blog.csdn.net/jiafu1115/article/details/6941245

    public List<String> executeNewFlow(List<String> commands) {
            List<String> rspList = new ArrayList<String>();
            Runtime run = Runtime.getRuntime();
            try {
                Process proc = run.exec("/bin/bash", null, null);
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
                for (String line : commands) {
                    out.println(line);
                }
                // out.println("cd /home/test");
                // out.println("pwd");
                // out.println("rm -fr /home/proxy.log");
                out.println("exit");// 这个命令必须执行,否则in流不结束。
                String rspLine = "";
                while ((rspLine = in.readLine()) != null) {
                    System.out.println(rspLine);
                    rspList.add(rspLine);
                }
                proc.waitFor();
                in.close();
                out.close();
                proc.destroy();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return rspList;
        }
    

      

  • 相关阅读:
    C++(四)--线程与进程
    http1.0升级到http1.1
    odoo 基础
    Ubuntu 上安装配置 Ldap
    odoo 怎样使代码生效
    Odoo 创建自定义模块
    开源的软件应用
    域控
    Flask 数据库 SQLAlchemy
    CentOS 8 防火墙 firewall 相关命令
  • 原文地址:https://www.cnblogs.com/GSONG/p/15343227.html
Copyright © 2011-2022 走看看