パッケージ com.kazurayam.subprocessj

クラス Subprocess

java.lang.Object
com.kazurayam.subprocessj.Subprocess

public class Subprocess extends Object

Subprocess object allows you to spawn new OS subprocess using java.lang.ProcessBuilder. Subprocess#run() waits for the subprocess to finish, returns a CompletedProcess object which contains the return code, STDOUT and STDIN of the executed command.

A simple example as a JUnit5 test case:

 import com.kazurayam.subprocessj.Subprocess;
 import com.kazurayam.subprocessj.Subprocess.CompletedProcess;
 import org.junit.jupiter.api.Test;

 class SubprocessTest {
     @Test
     void test_demo() throws Exception {

         Subprocess subprocess = new Subprocess();

         // change the current working directory for the process in which a command is executed
         subprocess.cwd(new File(System.getProperty("user.home")));

         // execute a command in a forked process
         CompletedProcess cp = subprocess.run(Arrays.asList("ls", "-la", "."));

         // use the return code of the executed command
         System.out.println(cp.returncode());

         // use the STDOUT of the executed command
         cp.stdout().forEach(System.out::println);

         // use the STDERR of the executed command
         cp.stderr().forEach(System.out::println);
     }
 }
 

The following features are supported:

  1. returning the return code from the Subpprocess
  2. capturing the STDOUT from the Subprocess
  3. capturing the STDERR from the Subprocess
  4. starting a subprocess with a modified working directory

The following features are still to be considered:

  1. PIPE: Connecting INPUT from other process as STDIN for the Subprocess
  2. Starting a Subprocess with a modified Environment variables
  3. inheriting the I/O of the Current Process
  • コンストラクタの詳細

    • Subprocess

      public Subprocess()
  • メソッドの詳細

    • environment

      public Map<String,String> environment()
    • environment

      public String environment(String key)
    • cwd

      public Subprocess cwd(File currentWorkingDirectory)
      Change the current working directory. As default, will be set as the current working directory of the caller process "."
      パラメータ:
      currentWorkingDirectory - a File object, which is a directory
      戻り値:
      the Subprocess object. for functional call chaining.
    • run

      Run the command written as a List<String>. Wait for the command to complete, then return a Subprocess.CompletedProcess instance, in which you can read: - the return code of the subprocess - the captured STDOUT of the subprocess - the captured STDERR of the subproess Referred to a Baeldung's article "Java Executor Wait for the Threads"
      パラメータ:
      command - E.g Array.asList("ls", "-la", ".")
      戻り値:
      a Subprocess.CompletedProcess instance
      例外:
      IOException - when failed to create a Thread to consume STDOUT/STDERR from the subprocess
      InterruptedException - when a Thread that consumes STDOUT/STDERR from the subprocess was interrupted