zoukankan      html  css  js  c++  java
  • java使用Runtime.exec()运行windwos dos或linux shell命令

    使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试
     
     
    实例代码:
    package com.bookoo.test.command;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
     
    /**
     *
     * @author nathan
     */
    public class TestLinuxCommand {
          public static void main(String[] args) {
          String logDir = System.getProperty("log.dir");
          
          
          // windows "/c"参数一定要加上 打开窗口立即关闭,获取读写流不会报异常
          String commands = "cmd.exe /c c: && dir";
          
          // linux
    //          String commands = "ls -l";
    //          String commands = "sh -c cd /data/log/daemon/bprocess && ls -lt";
    //          String[] commands = new String[] {"/bin/sh", "-c", "/bin/ls -lt /data/log/daemon/bprocess"};
     
          
          /**
           *  测试1:用命令字符串数组运行函数执行多个命令<br>
           *  运行流程:打开sh窗口-->执行pwd命令-->over<br>
           *  结果运行只运行了第一个命令<br>
           *  为什么了,经过测试发现,这个函数他根本就不用管有几个命令,只需要将这个数组组成一个字符串去运行,你数组的内容组成一条字符串是什么样,那运行结果就是什么样了(运行不了就报错误呗)<br>
           *  疑问:那这样的话,我如何运行多个命令了,查语法发现多个一起运行可以用"&&"符号,如:"dir &&dir d:&& e:"(空格是没有关系的)<br>
           *  好,上面是dos运行的情况,现在再看linux<br>
           *  shell只是数组中的第一条字符串pwd运行了,后面一个命令没有运行,原理跟dos是一样的,组成一个字符串运行有问题,加"&&"符号隔开两个命令就ok了<br>
           *  猜想:那好,如果只能运行一条字符串,那我不用数组了,看测试2
           */
          // windows dos
          // String[] commands = new String[] {"cmd.exe", "/c", "dir" ,"d:", "dir"};
          // String[] commands = new String[] {"cmd.exe", "/c", "dir" ,"&&d:&&", "dir"};
          // linux shell
          // 错误,还是只执行一个pwd命令,ls命令不执行,正确用法看测试2
          // String[] commands = new String[] {"/bin/sh", "-c", "pwd" , "/bin/ls"};
          // String[] commands = new String[] {"/bin/sh", "-c", "ls" , "&& pwd"};
          
          
          /**
           * 测试2:用命令字符串运行函数执行多个命令<br>
           * 运行流程:打开sh窗口-->执行pwd命令-->执行ls命令-->over<br>
           * 结果证明测试1结果猜想是正确的<br>
           */
          // windwos dows
          // String commands = "cmd.exe /c dir && c: && dir";
          // linux shell
          // ls命令不执行,参数没效果
          // String commands = "sh -c pwd&&ls -lt";
          // 正确,ls参数没效果
          // String commands = "sh -c ls -lt&&pwd";
          
          /**
           * 测试3:不同环境下切换目录命令<br>
           * windows可行,linux不可行<br>
           * 疑问:只要能够dos或者shell中测试的一整条命令,exec就能运行,但linux切换目录命令一加就不行<br>
           */
          // windows
          // String commands = "cmd.exe /c dir && c: && dir";
          // linux
          // 错误
          // String commands = "sh -c pwd && cd /data/log/daemon/bprocess && ls -lt";
          // 正确,ls参数没生效
          // String commands = "sh -c ls -lt /data/log/daemon/bprocess && pwd";
          // 正确,ls参数生效,神奇吧,前面不用加sh一样能运行且这样运行ls参数生效,但是pwd命令用不了,原因没在schell环境中,单独运行加sh -c 就行了
          // String commands = "/bin/ls -lt /data/log/daemon/bprocess && pwd";
          // 错误
          // String[] commands = new String[] {"/bin/sh", "-c", "pwd" ,"cd /data/log/daemon/bprocess", "/bin/ls"};
          
          // 这样的话,schell环境下执行,我要想看哪个目录文件,就不能先切换过去在看了,命令写成这样:/bin/ls -lt /data/log/daemon/bprocess
          // dos也是一样的 dir c:
          
          /**
           * 最后<br>
           * cmd.exe=cmd(大写也行),sh=/bin/sh(schell命令都是在/bin目录下),操作结果都是一样的,不会报错<br>
           * /bin/sh这种写法执行为了调用更安全(直接告诉命令在哪,不要linux自己再去识别一下了,我是这样猜想的)<br>
           */
          java.lang.Process process = null;
          try
                {
                System.out.println("exec commands success");
                process = Runtime.getRuntime().exec(commands);
                InputStreamReader inputStream = new InputStreamReader(process.getInputStream());
                BufferedReader input = new BufferedReader(inputStream);
                String line;
                System.out.println("print inputStream start");
                while ((line = input.readLine()) != null){
                      System.out.println(line);
                }
                System.out.println("print inputStream over");
                
    //                InputStreamReader errorStream = new InputStreamReader(process.getErrorStream());
    //                input = new BufferedReader(errorStream);
    //                System.out.println("print errorStream start");
    //                while ((line = input.readLine()) != null){
    //                      System.out.println(line);
    //                      input.close();
    //                }
    //                System.out.println("print errorStream over");
                
    //                OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream());
    //                BufferedWriter writer = new BufferedWriter(outputStream);
    //                System.out.println("print outputStream start");
    //                writer.write("outputStream writer success");
    //                writer.flush();
    //                writer.close();
    //                System.out.println("print outputStream over");
                } catch (IOException e)
                {
                      System.out.println("have ioexception");
                      e.printStackTrace();
                } finally{
                      try
                      {
                            System.out.println(process.waitFor());
                      } catch (InterruptedException e)
                      {
                            e.printStackTrace();
                      }
                }
          }
    }
     
     
     
     
    搜索关键词:java 运行linux命令
  • 相关阅读:
    使用 RestSharp 调用 WebAPI 接口
    Android Studio 下载安装目录
    多线程之await/async
    ScriptX进行Web打印
    Sqlserver 查询最新修改过的表、过程和视图等
    SqlServer中的bit类型
    .Net 6
    PDA 使用总结
    SQL Server 发布订阅 发布类型详解
    Profile对象
  • 原文地址:https://www.cnblogs.com/svennee/p/4082877.html
Copyright © 2011-2022 走看看