パッケージ com.kazurayam.subprocessj

クラス Subprocess


  • public class Subprocess
    extends java.lang.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
    • ネストされたクラスの概要

      ネストされたクラス 
      修飾子とタイプ クラス 説明
      static class  Subprocess.CompletedProcess
      A Data Transfer Object that contains the return code, STDOUT and STDERR out of the executed subprocess.
      static class  Subprocess.StreamGobbler  
    • コンストラクタの概要

      コンストラクタ 
      コンストラクタ 説明
      Subprocess()  
    • メソッドの概要

      すべてのメソッド インスタンス・メソッド concreteメソッド 
      修飾子とタイプ メソッド 説明
      Subprocess cwd​(java.io.File currentWorkingDirectory)
      Change the current working directory.
      java.util.Map<java.lang.String,​java.lang.String> environment()  
      java.lang.String environment​(java.lang.String key)  
      Subprocess.CompletedProcess run​(java.util.List<java.lang.String> command)
      Run the command written as a List<String>.
      • クラスから継承されたメソッド java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • コンストラクタの詳細

      • Subprocess

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

      • environment

        public java.util.Map<java.lang.String,​java.lang.String> environment()
      • environment

        public java.lang.String environment​(java.lang.String key)
      • cwd

        public Subprocess cwd​(java.io.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

        public Subprocess.CompletedProcess run​(java.util.List<java.lang.String> command)
                                        throws java.io.IOException,
                                               java.lang.InterruptedException
        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
        例外:
        java.io.IOException - when failed to create a Thread to consume STDOUT/STDERR from the subprocess
        java.lang.InterruptedException - when a Thread that consumes STDOUT/STDERR from the subprocess was interrupted