How Java calls external program based on processbuilder class

Time:2020-12-8

This article mainly introduces how Java calls external programs based on processbuilder class. The example code is introduced in detail, which has certain reference value for everyone’s study or work. Friends in need can refer to it

1. demo1

@Test
  public void testProcessBuilder() {
    ProcessBuilder processBuilder = new ProcessBuilder();
//    processBuilder.command("ping","127.0.0.1");
    processBuilder.command("ipconfig");
    //The standard input stream is combined with the error input stream, and the information is read through the standard input stream
    processBuilder.redirectErrorStream(true);
    try {
      //Start the process
      Process start = processBuilder.start();
      //Get input stream
      InputStream inputStream = start.getInputStream();
      //Convert to character input stream
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
      int len = -1;
      char[] c = new char[1024];
      StringBuffer outputString = new StringBuffer();
      //Read the contents of the process input stream
      while ((len = inputStreamReader.read(c)) != -1) {
        String s = new String(c, 0, len);
        outputString.append(s);
        System.out.print(s);
      }
      inputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

2. demo02

@Test
  public void testFFmpeg() {
    ProcessBuilder processBuilder = new ProcessBuilder();
    //Define command content
    List<String> command = new ArrayList<>();
    command.add("D:/program/ffmpeg-20191229-e20c6d9-win64-static/bin/ffmpeg.exe");
    command.add("-i");
    command.add("D:/test/video/1.avi");
    command.add ("- Y"); // overlay output file
    command.add("-c:v");
    command.add("libx264");
    command.add("-s");
    command.add("1280x720");
    command.add("-pix_fmt");
    command.add("yuv420p");
    command.add("-b:a");
    command.add("63k");
    command.add("-b:v");
    command.add("753k");
    command.add("-r");
    command.add("18");
    command.add("D:/test/video/1.mp4");
    processBuilder.command(command);
    //The standard input stream is combined with the error input stream, and the information is read through the standard input stream
    processBuilder.redirectErrorStream(true);
    try {
      //Start the process
      Process start = processBuilder.start();
      //Get input stream
      InputStream inputStream = start.getInputStream();
      //Convert to character input stream
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
      int len = -1;
      char[] c = new char[1024];
      StringBuffer outputString = new StringBuffer();
      //Read the contents of the process input stream
      while ((len = inputStreamReader.read(c)) != -1) {
        String s = new String(c, 0, len);
        outputString.append(s);
        System.out.print(s);
      }
      inputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.