jdk/src/java.base/share/classes/java/lang/ProcessBuilder.java
author simonis
Fri, 14 Aug 2015 10:35:45 +0200
changeset 32209 24bb680a1609
parent 29601 d6e9fbd8497a
child 32649 2ee9017c7597
permissions -rw-r--r--
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX Reviewed-by: rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 19606
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5186
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5186
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5186
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5186
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5186
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    30
import java.io.InputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    31
import java.io.OutputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    32
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class is used to create operating system processes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    40
 * <p>Each {@code ProcessBuilder} instance manages a collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of process attributes.  The {@link #start()} method creates a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@link Process} instance with those attributes.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * #start()} method can be invoked repeatedly from the same instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to create new subprocesses with identical or related attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>Each process builder manages these process attributes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li>a <i>command</i>, a list of strings which signifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * external program file to be invoked and its arguments, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Which string lists represent a valid operating system command is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * system-dependent.  For example, it is common for each conceptual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * argument to be an element in this list, but there are operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * systems where programs are expected to tokenize command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * strings themselves - on such a system a Java implementation might
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * require commands to contain exactly two elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <li>an <i>environment</i>, which is a system-dependent mapping from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <i>variables</i> to <i>values</i>.  The initial value is a copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * the environment of the current process (see {@link System#getenv()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <li>a <i>working directory</i>.  The default value is the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * working directory of the current process, usually the directory
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    65
 * named by the system property {@code user.dir}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    66
 *
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 17467
diff changeset
    67
 * <li><a name="redirect-input">a source of <i>standard input</i></a>.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    68
 * By default, the subprocess reads input from a pipe.  Java code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    69
 * can access this pipe via the output stream returned by
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    70
 * {@link Process#getOutputStream()}.  However, standard input may
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    71
 * be redirected to another source using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    72
 * {@link #redirectInput(Redirect) redirectInput}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    73
 * In this case, {@link Process#getOutputStream()} will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    74
 * <i>null output stream</i>, for which:
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    75
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    76
 * <ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    77
 * <li>the {@link OutputStream#write(int) write} methods always
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    78
 * throw {@code IOException}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    79
 * <li>the {@link OutputStream#close() close} method does nothing
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    80
 * </ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    81
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    82
 * <li><a name="redirect-output">a destination for <i>standard output</i>
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 17467
diff changeset
    83
 * and <i>standard error</i></a>.  By default, the subprocess writes standard
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    84
 * output and standard error to pipes.  Java code can access these pipes
29601
d6e9fbd8497a 8073220: A typo in the documentation for class ProcessBuilder
rriggs
parents: 29600
diff changeset
    85
 * via the input streams returned by {@link Process#getOutputStream()} and
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    86
 * {@link Process#getErrorStream()}.  However, standard output and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    87
 * standard error may be redirected to other destinations using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    88
 * {@link #redirectOutput(Redirect) redirectOutput} and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    89
 * {@link #redirectError(Redirect) redirectError}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    90
 * In this case, {@link Process#getInputStream()} and/or
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    91
 * {@link Process#getErrorStream()} will return a <i>null input
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    92
 * stream</i>, for which:
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    93
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    94
 * <ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    95
 * <li>the {@link InputStream#read() read} methods always return
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    96
 * {@code -1}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    97
 * <li>the {@link InputStream#available() available} method always returns
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    98
 * {@code 0}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    99
 * <li>the {@link InputStream#close() close} method does nothing
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   100
 * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <li>a <i>redirectErrorStream</i> property.  Initially, this property
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   103
 * is {@code false}, meaning that the standard output and error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * output of a subprocess are sent to two separate streams, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * be accessed using the {@link Process#getInputStream()} and {@link
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   106
 * Process#getErrorStream()} methods.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   107
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   108
 * <p>If the value is set to {@code true}, then:
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   109
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   110
 * <ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   111
 * <li>standard error is merged with the standard output and always sent
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   112
 * to the same destination (this makes it easier to correlate error
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   113
 * messages with the corresponding output)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   114
 * <li>the common destination of standard error and standard output can be
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   115
 * redirected using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   116
 * {@link #redirectOutput(Redirect) redirectOutput}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   117
 * <li>any redirection set by the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   118
 * {@link #redirectError(Redirect) redirectError}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   119
 * method is ignored when creating a subprocess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   120
 * <li>the stream returned from {@link Process#getErrorStream()} will
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   121
 * always be a <a href="#redirect-output">null input stream</a>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   122
 * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p>Modifying a process builder's attributes will affect processes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * subsequently started by that object's {@link #start()} method, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * will never affect previously started processes or the Java process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <p>Most error checking is performed by the {@link #start()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * It is possible to modify the state of an object so that {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * #start()} will fail.  For example, setting the command attribute to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * an empty list will not throw an exception unless {@link #start()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <p><strong>Note that this class is not synchronized.</strong>
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   138
 * If multiple threads access a {@code ProcessBuilder} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * concurrently, and at least one of the threads modifies one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * attributes structurally, it <i>must</i> be synchronized externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>Starting a new process which uses the default working directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * and environment is easy:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   145
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * Process p = new ProcessBuilder("myCommand", "myArg").start();
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   147
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p>Here is an example that starts a process with a modified working
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   150
 * directory and environment, and redirects standard output and error
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   151
 * to be appended to a log file:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   153
 * <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   154
 * ProcessBuilder pb =
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   155
 *   new ProcessBuilder("myCommand", "myArg1", "myArg2");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   156
 * Map<String, String> env = pb.environment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * env.put("VAR1", "myValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * env.remove("OTHERVAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * env.put("VAR2", env.get("VAR1") + "suffix");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * pb.directory(new File("myDir"));
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   161
 * File log = new File("log");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   162
 * pb.redirectErrorStream(true);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   163
 * pb.redirectOutput(Redirect.appendTo(log));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * Process p = pb.start();
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   165
 * assert pb.redirectInput() == Redirect.PIPE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   166
 * assert pb.redirectOutput().file() == log;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   167
 * assert p.getInputStream().read() == -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   168
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * <p>To start a process with an explicit set of environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * variables, first call {@link java.util.Map#clear() Map.clear()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * before adding environment variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
29381
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28872
diff changeset
   174
 * <p>
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28872
diff changeset
   175
 * Unless otherwise noted, passing a {@code null} argument to a constructor
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28872
diff changeset
   176
 * or method in this class will cause a {@link NullPointerException} to be
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28872
diff changeset
   177
 * thrown.
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28872
diff changeset
   178
 *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   179
 * @author Martin Buchholz
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
public final class ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private List<String> command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private File directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private Map<String,String> environment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private boolean redirectErrorStream;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   189
    private Redirect[] redirects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Constructs a process builder with the specified operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * system program and arguments.  This constructor does <i>not</i>
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   194
     * make a copy of the {@code command} list.  Subsequent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * updates to the list will be reflected in the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * process builder.  It is not checked whether
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   197
     * {@code command} corresponds to a valid operating system
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   198
     * command.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   200
     * @param  command the list containing the program and its arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public ProcessBuilder(List<String> command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (command == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.command = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Constructs a process builder with the specified operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * system program and arguments.  This is a convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * constructor that sets the process builder's command to a string
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   212
     * list containing the same strings as the {@code command}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * array, in the same order.  It is not checked whether
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   214
     * {@code command} corresponds to a valid operating system
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   215
     * command.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   217
     * @param command a string array containing the program and its arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public ProcessBuilder(String... command) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7515
diff changeset
   220
        this.command = new ArrayList<>(command.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (String arg : command)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            this.command.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Sets this process builder's operating system program and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * arguments.  This method does <i>not</i> make a copy of the
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   228
     * {@code command} list.  Subsequent updates to the list will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * be reflected in the state of the process builder.  It is not
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   230
     * checked whether {@code command} corresponds to a valid
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   231
     * operating system command.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   233
     * @param  command the list containing the program and its arguments
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   234
     * @return this process builder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public ProcessBuilder command(List<String> command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (command == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        this.command = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Sets this process builder's operating system program and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * arguments.  This is a convenience method that sets the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * to a string list containing the same strings as the
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   247
     * {@code command} array, in the same order.  It is not
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   248
     * checked whether {@code command} corresponds to a valid
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   249
     * operating system command.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   251
     * @param  command a string array containing the program and its arguments
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   252
     * @return this process builder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public ProcessBuilder command(String... command) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7515
diff changeset
   255
        this.command = new ArrayList<>(command.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        for (String arg : command)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            this.command.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns this process builder's operating system program and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * arguments.  The returned list is <i>not</i> a copy.  Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * updates to the list will be reflected in the state of this
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   265
     * process builder.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   267
     * @return this process builder's program and its arguments
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public List<String> command() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Returns a string map view of this process builder's environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Whenever a process builder is created, the environment is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * initialized to a copy of the current process environment (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * {@link System#getenv()}).  Subprocesses subsequently started by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * this object's {@link #start()} method will use this map as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * their environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>The returned object may be modified using ordinary {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * java.util.Map Map} operations.  These modifications will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * visible to subprocesses started via the {@link #start()}
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   285
     * method.  Two {@code ProcessBuilder} instances always
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * contain independent process environments, so changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * returned map will never be reflected in any other
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   288
     * {@code ProcessBuilder} instance or the values returned by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * {@link System#getenv System.getenv}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>If the system does not support environment variables, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * empty map is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p>The returned map does not permit null keys or values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Attempting to insert or query the presence of a null key or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * value will throw a {@link NullPointerException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Attempting to query the presence of a key or value which is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * of type {@link String} will throw a {@link ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>The behavior of the returned map is system-dependent.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * system may not allow modifications to environment variables or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * may forbid certain variable names or values.  For this reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * attempts to modify the map may fail with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@link UnsupportedOperationException} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * {@link IllegalArgumentException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * if the modification is not permitted by the operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <p>Since the external format of environment variable names and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * values is system-dependent, there may not be a one-to-one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * mapping between them and Java's Unicode strings.  Nevertheless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * the map is implemented in such a way that environment variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * which are not modified by Java code will have an unmodified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * native representation in the subprocess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>The returned map and its collection views may not obey the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * general contract of the {@link Object#equals} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * {@link Object#hashCode} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>The returned map is typically case-sensitive on all platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <p>If a security manager exists, its
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   322
     * {@link SecurityManager#checkPermission checkPermission} method
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   323
     * is called with a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   324
     * {@link RuntimePermission}{@code ("getenv.*")} permission.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   325
     * This may result in a {@link SecurityException} being thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>When passing information to a Java subprocess,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <a href=System.html#EnvironmentVSSystemProperties>system properties</a>
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   329
     * are generally preferred over environment variables.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   331
     * @return this process builder's environment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   333
     * @throws SecurityException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   334
     *         if a security manager exists and its
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   335
     *         {@link SecurityManager#checkPermission checkPermission}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   336
     *         method doesn't allow access to the process environment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   338
     * @see    Runtime#exec(String[],String[],java.io.File)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   339
     * @see    System#getenv()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public Map<String,String> environment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (security != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            security.checkPermission(new RuntimePermission("getenv.*"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (environment == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            environment = ProcessEnvironment.environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        assert environment != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return environment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    // Only for use by Runtime.exec(...envp...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    ProcessBuilder environment(String[] envp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        assert environment == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (envp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            environment = ProcessEnvironment.emptyEnvironment(envp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            assert environment != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            for (String envstring : envp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                // Before 1.5, we blindly passed invalid envstrings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                // to the child process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // We would like to throw an exception, but do not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                // for compatibility with old broken code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                // Silently discard any trailing junk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                if (envstring.indexOf((int) '\u0000') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    envstring = envstring.replaceFirst("\u0000.*", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                int eqlsign =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    envstring.indexOf('=', ProcessEnvironment.MIN_NAME_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                // Silently ignore envstrings lacking the required `='.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (eqlsign != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    environment.put(envstring.substring(0,eqlsign),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                    envstring.substring(eqlsign+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Returns this process builder's working directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Subprocesses subsequently started by this object's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * #start()} method will use this as their working directory.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   387
     * The returned value may be {@code null} -- this means to use
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * the working directory of the current Java process, usually the
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   389
     * directory named by the system property {@code user.dir},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   390
     * as the working directory of the child process.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   392
     * @return this process builder's working directory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public File directory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Sets this process builder's working directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Subprocesses subsequently started by this object's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * #start()} method will use this as their working directory.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   403
     * The argument may be {@code null} -- this means to use the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * working directory of the current Java process, usually the
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   405
     * directory named by the system property {@code user.dir},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   406
     * as the working directory of the child process.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   408
     * @param  directory the new working directory
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   409
     * @return this process builder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public ProcessBuilder directory(File directory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        this.directory = directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   416
    // ---------------- I/O Redirection ----------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   417
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   418
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   419
     * Implements a <a href="#redirect-output">null input stream</a>.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   420
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   421
    static class NullInputStream extends InputStream {
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5506
diff changeset
   422
        static final NullInputStream INSTANCE = new NullInputStream();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5506
diff changeset
   423
        private NullInputStream() {}
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   424
        public int read()      { return -1; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   425
        public int available() { return 0; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   426
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   427
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   428
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   429
     * Implements a <a href="#redirect-input">null output stream</a>.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   430
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   431
    static class NullOutputStream extends OutputStream {
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5506
diff changeset
   432
        static final NullOutputStream INSTANCE = new NullOutputStream();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5506
diff changeset
   433
        private NullOutputStream() {}
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   434
        public void write(int b) throws IOException {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   435
            throw new IOException("Stream closed");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   436
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   437
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   438
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   439
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   440
     * Represents a source of subprocess input or a destination of
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   441
     * subprocess output.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   442
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   443
     * Each {@code Redirect} instance is one of the following:
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   444
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   445
     * <ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   446
     * <li>the special value {@link #PIPE Redirect.PIPE}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   447
     * <li>the special value {@link #INHERIT Redirect.INHERIT}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   448
     * <li>a redirection to read from a file, created by an invocation of
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   449
     *     {@link Redirect#from Redirect.from(File)}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   450
     * <li>a redirection to write to a file,  created by an invocation of
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   451
     *     {@link Redirect#to Redirect.to(File)}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   452
     * <li>a redirection to append to a file, created by an invocation of
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   453
     *     {@link Redirect#appendTo Redirect.appendTo(File)}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   454
     * </ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   455
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   456
     * <p>Each of the above categories has an associated unique
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   457
     * {@link Type Type}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   458
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   459
     * @since 1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   460
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   461
    public static abstract class Redirect {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   462
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   463
         * The type of a {@link Redirect}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   464
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   465
        public enum Type {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   466
            /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   467
             * The type of {@link Redirect#PIPE Redirect.PIPE}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   468
             */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   469
            PIPE,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   470
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   471
            /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   472
             * The type of {@link Redirect#INHERIT Redirect.INHERIT}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   473
             */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   474
            INHERIT,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   475
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   476
            /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   477
             * The type of redirects returned from
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   478
             * {@link Redirect#from Redirect.from(File)}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   479
             */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   480
            READ,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   481
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   482
            /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   483
             * The type of redirects returned from
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   484
             * {@link Redirect#to Redirect.to(File)}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   485
             */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   486
            WRITE,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   487
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   488
            /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   489
             * The type of redirects returned from
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   490
             * {@link Redirect#appendTo Redirect.appendTo(File)}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   491
             */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   492
            APPEND
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   493
        };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   494
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   495
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   496
         * Returns the type of this {@code Redirect}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   497
         * @return the type of this {@code Redirect}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   498
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   499
        public abstract Type type();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   500
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   501
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   502
         * Indicates that subprocess I/O will be connected to the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   503
         * current Java process over a pipe.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   504
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   505
         * This is the default handling of subprocess standard I/O.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   506
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   507
         * <p>It will always be true that
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   508
         *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   509
         * Redirect.PIPE.file() == null &&
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   510
         * Redirect.PIPE.type() == Redirect.Type.PIPE
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   511
         * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   512
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   513
        public static final Redirect PIPE = new Redirect() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   514
                public Type type() { return Type.PIPE; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   515
                public String toString() { return type().toString(); }};
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   516
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   517
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   518
         * Indicates that subprocess I/O source or destination will be the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   519
         * same as those of the current process.  This is the normal
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   520
         * behavior of most operating system command interpreters (shells).
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   521
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   522
         * <p>It will always be true that
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   523
         *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   524
         * Redirect.INHERIT.file() == null &&
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   525
         * Redirect.INHERIT.type() == Redirect.Type.INHERIT
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   526
         * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   527
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   528
        public static final Redirect INHERIT = new Redirect() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   529
                public Type type() { return Type.INHERIT; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   530
                public String toString() { return type().toString(); }};
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   531
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   532
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   533
         * Returns the {@link File} source or destination associated
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   534
         * with this redirect, or {@code null} if there is no such file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   535
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   536
         * @return the file associated with this redirect,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   537
         *         or {@code null} if there is no such file
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   538
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   539
        public File file() { return null; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   540
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   541
        /**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   542
         * When redirected to a destination file, indicates if the output
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   543
         * is to be written to the end of the file.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   544
         */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   545
        boolean append() {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   546
            throw new UnsupportedOperationException();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   547
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   548
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   549
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   550
         * Returns a redirect to read from the specified file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   551
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   552
         * <p>It will always be true that
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   553
         *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   554
         * Redirect.from(file).file() == file &&
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   555
         * Redirect.from(file).type() == Redirect.Type.READ
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   556
         * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   557
         *
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 17467
diff changeset
   558
         * @param file The {@code File} for the {@code Redirect}.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   559
         * @return a redirect to read from the specified file
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   560
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   561
        public static Redirect from(final File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   562
            if (file == null)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   563
                throw new NullPointerException();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   564
            return new Redirect() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   565
                    public Type type() { return Type.READ; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   566
                    public File file() { return file; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   567
                    public String toString() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   568
                        return "redirect to read from file \"" + file + "\"";
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   569
                    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   570
                };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   571
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   572
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   573
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   574
         * Returns a redirect to write to the specified file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   575
         * If the specified file exists when the subprocess is started,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   576
         * its previous contents will be discarded.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   577
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   578
         * <p>It will always be true that
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   579
         *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   580
         * Redirect.to(file).file() == file &&
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   581
         * Redirect.to(file).type() == Redirect.Type.WRITE
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   582
         * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   583
         *
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 17467
diff changeset
   584
         * @param file The {@code File} for the {@code Redirect}.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   585
         * @return a redirect to write to the specified file
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   586
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   587
        public static Redirect to(final File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   588
            if (file == null)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   589
                throw new NullPointerException();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   590
            return new Redirect() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   591
                    public Type type() { return Type.WRITE; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   592
                    public File file() { return file; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   593
                    public String toString() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   594
                        return "redirect to write to file \"" + file + "\"";
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   595
                    }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   596
                    boolean append() { return false; }
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   597
                };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   598
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   599
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   600
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   601
         * Returns a redirect to append to the specified file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   602
         * Each write operation first advances the position to the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   603
         * end of the file and then writes the requested data.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   604
         * Whether the advancement of the position and the writing
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   605
         * of the data are done in a single atomic operation is
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   606
         * system-dependent and therefore unspecified.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   607
         *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   608
         * <p>It will always be true that
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   609
         *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   610
         * Redirect.appendTo(file).file() == file &&
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   611
         * Redirect.appendTo(file).type() == Redirect.Type.APPEND
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   612
         * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   613
         *
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 17467
diff changeset
   614
         * @param file The {@code File} for the {@code Redirect}.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   615
         * @return a redirect to append to the specified file
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   616
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   617
        public static Redirect appendTo(final File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   618
            if (file == null)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   619
                throw new NullPointerException();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   620
            return new Redirect() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   621
                    public Type type() { return Type.APPEND; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   622
                    public File file() { return file; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   623
                    public String toString() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   624
                        return "redirect to append to file \"" + file + "\"";
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   625
                    }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5786
diff changeset
   626
                    boolean append() { return true; }
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   627
                };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   628
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   629
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   630
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   631
         * Compares the specified object with this {@code Redirect} for
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   632
         * equality.  Returns {@code true} if and only if the two
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   633
         * objects are identical or both objects are {@code Redirect}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   634
         * instances of the same type associated with non-null equal
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   635
         * {@code File} instances.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   636
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   637
        public boolean equals(Object obj) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   638
            if (obj == this)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   639
                return true;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   640
            if (! (obj instanceof Redirect))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   641
                return false;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   642
            Redirect r = (Redirect) obj;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   643
            if (r.type() != this.type())
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   644
                return false;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   645
            assert this.file() != null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   646
            return this.file().equals(r.file());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   647
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   648
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   649
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   650
         * Returns a hash code value for this {@code Redirect}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   651
         * @return a hash code value for this {@code Redirect}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   652
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   653
        public int hashCode() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   654
            File file = file();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   655
            if (file == null)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   656
                return super.hashCode();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   657
            else
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   658
                return file.hashCode();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   659
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   660
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   661
        /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   662
         * No public constructors.  Clients must use predefined
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   663
         * static {@code Redirect} instances or factory methods.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   664
         */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   665
        private Redirect() {}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   666
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   667
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   668
    private Redirect[] redirects() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   669
        if (redirects == null)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   670
            redirects = new Redirect[] {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   671
                Redirect.PIPE, Redirect.PIPE, Redirect.PIPE
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   672
            };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   673
        return redirects;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   674
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   675
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   676
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   677
     * Sets this process builder's standard input source.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   678
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   679
     * Subprocesses subsequently started by this object's {@link #start()}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   680
     * method obtain their standard input from this source.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   681
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   682
     * <p>If the source is {@link Redirect#PIPE Redirect.PIPE}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   683
     * (the initial value), then the standard input of a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   684
     * subprocess can be written to using the output stream
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   685
     * returned by {@link Process#getOutputStream()}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   686
     * If the source is set to any other value, then
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   687
     * {@link Process#getOutputStream()} will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   688
     * <a href="#redirect-input">null output stream</a>.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   689
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   690
     * @param  source the new standard input source
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   691
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   692
     * @throws IllegalArgumentException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   693
     *         if the redirect does not correspond to a valid source
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   694
     *         of data, that is, has type
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   695
     *         {@link Redirect.Type#WRITE WRITE} or
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   696
     *         {@link Redirect.Type#APPEND APPEND}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   697
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   698
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   699
    public ProcessBuilder redirectInput(Redirect source) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   700
        if (source.type() == Redirect.Type.WRITE ||
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   701
            source.type() == Redirect.Type.APPEND)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   702
            throw new IllegalArgumentException(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   703
                "Redirect invalid for reading: " + source);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   704
        redirects()[0] = source;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   705
        return this;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   706
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   707
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   708
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   709
     * Sets this process builder's standard output destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   710
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   711
     * Subprocesses subsequently started by this object's {@link #start()}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   712
     * method send their standard output to this destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   713
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   714
     * <p>If the destination is {@link Redirect#PIPE Redirect.PIPE}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   715
     * (the initial value), then the standard output of a subprocess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   716
     * can be read using the input stream returned by {@link
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   717
     * Process#getInputStream()}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   718
     * If the destination is set to any other value, then
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   719
     * {@link Process#getInputStream()} will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   720
     * <a href="#redirect-output">null input stream</a>.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   721
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   722
     * @param  destination the new standard output destination
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   723
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   724
     * @throws IllegalArgumentException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   725
     *         if the redirect does not correspond to a valid
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   726
     *         destination of data, that is, has type
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   727
     *         {@link Redirect.Type#READ READ}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   728
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   729
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   730
    public ProcessBuilder redirectOutput(Redirect destination) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   731
        if (destination.type() == Redirect.Type.READ)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   732
            throw new IllegalArgumentException(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   733
                "Redirect invalid for writing: " + destination);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   734
        redirects()[1] = destination;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   735
        return this;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   736
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   737
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   738
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   739
     * Sets this process builder's standard error destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   740
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   741
     * Subprocesses subsequently started by this object's {@link #start()}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   742
     * method send their standard error to this destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   743
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   744
     * <p>If the destination is {@link Redirect#PIPE Redirect.PIPE}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   745
     * (the initial value), then the error output of a subprocess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   746
     * can be read using the input stream returned by {@link
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   747
     * Process#getErrorStream()}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   748
     * If the destination is set to any other value, then
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   749
     * {@link Process#getErrorStream()} will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   750
     * <a href="#redirect-output">null input stream</a>.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   751
     *
28058
87940c838900 8056238: (process) ProcessBuilder.redirectError spec has a broken link
rriggs
parents: 25859
diff changeset
   752
     * <p>If the {@link #redirectErrorStream() redirectErrorStream}
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   753
     * attribute has been set {@code true}, then the redirection set
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   754
     * by this method has no effect.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   755
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   756
     * @param  destination the new standard error destination
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   757
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   758
     * @throws IllegalArgumentException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   759
     *         if the redirect does not correspond to a valid
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   760
     *         destination of data, that is, has type
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   761
     *         {@link Redirect.Type#READ READ}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   762
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   763
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   764
    public ProcessBuilder redirectError(Redirect destination) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   765
        if (destination.type() == Redirect.Type.READ)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   766
            throw new IllegalArgumentException(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   767
                "Redirect invalid for writing: " + destination);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   768
        redirects()[2] = destination;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   769
        return this;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   770
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   771
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   772
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   773
     * Sets this process builder's standard input source to a file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   774
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   775
     * <p>This is a convenience method.  An invocation of the form
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   776
     * {@code redirectInput(file)}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   777
     * behaves in exactly the same way as the invocation
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   778
     * {@link #redirectInput(Redirect) redirectInput}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   779
     * {@code (Redirect.from(file))}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   780
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   781
     * @param  file the new standard input source
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   782
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   783
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   784
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   785
    public ProcessBuilder redirectInput(File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   786
        return redirectInput(Redirect.from(file));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   787
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   788
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   789
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   790
     * Sets this process builder's standard output destination to a file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   791
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   792
     * <p>This is a convenience method.  An invocation of the form
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   793
     * {@code redirectOutput(file)}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   794
     * behaves in exactly the same way as the invocation
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   795
     * {@link #redirectOutput(Redirect) redirectOutput}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   796
     * {@code (Redirect.to(file))}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   797
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   798
     * @param  file the new standard output destination
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   799
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   800
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   801
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   802
    public ProcessBuilder redirectOutput(File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   803
        return redirectOutput(Redirect.to(file));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   804
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   805
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   806
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   807
     * Sets this process builder's standard error destination to a file.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   808
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   809
     * <p>This is a convenience method.  An invocation of the form
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   810
     * {@code redirectError(file)}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   811
     * behaves in exactly the same way as the invocation
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   812
     * {@link #redirectError(Redirect) redirectError}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   813
     * {@code (Redirect.to(file))}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   814
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   815
     * @param  file the new standard error destination
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   816
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   817
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   818
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   819
    public ProcessBuilder redirectError(File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   820
        return redirectError(Redirect.to(file));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   821
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   822
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   823
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   824
     * Returns this process builder's standard input source.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   825
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   826
     * Subprocesses subsequently started by this object's {@link #start()}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   827
     * method obtain their standard input from this source.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   828
     * The initial value is {@link Redirect#PIPE Redirect.PIPE}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   829
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   830
     * @return this process builder's standard input source
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   831
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   832
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   833
    public Redirect redirectInput() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   834
        return (redirects == null) ? Redirect.PIPE : redirects[0];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   835
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   836
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   837
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   838
     * Returns this process builder's standard output destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   839
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   840
     * Subprocesses subsequently started by this object's {@link #start()}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   841
     * method redirect their standard output to this destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   842
     * The initial value is {@link Redirect#PIPE Redirect.PIPE}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   843
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   844
     * @return this process builder's standard output destination
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   845
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   846
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   847
    public Redirect redirectOutput() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   848
        return (redirects == null) ? Redirect.PIPE : redirects[1];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   849
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   850
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   851
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   852
     * Returns this process builder's standard error destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   853
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   854
     * Subprocesses subsequently started by this object's {@link #start()}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   855
     * method redirect their standard error to this destination.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   856
     * The initial value is {@link Redirect#PIPE Redirect.PIPE}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   857
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   858
     * @return this process builder's standard error destination
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   859
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   860
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   861
    public Redirect redirectError() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   862
        return (redirects == null) ? Redirect.PIPE : redirects[2];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   863
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   864
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   865
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   866
     * Sets the source and destination for subprocess standard I/O
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   867
     * to be the same as those of the current Java process.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   868
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   869
     * <p>This is a convenience method.  An invocation of the form
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   870
     *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   871
     * pb.inheritIO()
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   872
     * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   873
     * behaves in exactly the same way as the invocation
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   874
     *  <pre> {@code
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   875
     * pb.redirectInput(Redirect.INHERIT)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   876
     *   .redirectOutput(Redirect.INHERIT)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   877
     *   .redirectError(Redirect.INHERIT)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   878
     * }</pre>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   879
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   880
     * This gives behavior equivalent to most operating system
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   881
     * command interpreters, or the standard C library function
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   882
     * {@code system()}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   883
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   884
     * @return this process builder
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   885
     * @since  1.7
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   886
     */
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   887
    public ProcessBuilder inheritIO() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   888
        Arrays.fill(redirects(), Redirect.INHERIT);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   889
        return this;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   890
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   891
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * Tells whether this process builder merges standard error and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * standard output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   896
     * <p>If this property is {@code true}, then any error output
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * generated by subprocesses subsequently started by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * {@link #start()} method will be merged with the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * output, so that both can be read using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * {@link Process#getInputStream()} method.  This makes it easier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * to correlate error messages with the corresponding output.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   902
     * The initial value is {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   904
     * @return this process builder's {@code redirectErrorStream} property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public boolean redirectErrorStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        return redirectErrorStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /**
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   911
     * Sets this process builder's {@code redirectErrorStream} property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   913
     * <p>If this property is {@code true}, then any error output
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * generated by subprocesses subsequently started by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * {@link #start()} method will be merged with the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * output, so that both can be read using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * {@link Process#getInputStream()} method.  This makes it easier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * to correlate error messages with the corresponding output.
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   919
     * The initial value is {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   921
     * @param  redirectErrorStream the new property value
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   922
     * @return this process builder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public ProcessBuilder redirectErrorStream(boolean redirectErrorStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        this.redirectErrorStream = redirectErrorStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Starts a new process using the attributes of this process builder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <p>The new process will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * invoke the command and arguments given by {@link #command()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * in a working directory as given by {@link #directory()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * with a process environment as given by {@link #environment()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * <p>This method checks that the command is a valid operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * system command.  Which commands are valid is system-dependent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * but at the very least the command must be a non-empty list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * non-null strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
9500
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 7816
diff changeset
   942
     * <p>A minimal set of system dependent environment variables may
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 7816
diff changeset
   943
     * be required to start a process on some operating systems.
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 7816
diff changeset
   944
     * As a result, the subprocess may inherit additional environment variable
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 7816
diff changeset
   945
     * settings beyond those in the process builder's {@link #environment()}.
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 7816
diff changeset
   946
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * <p>If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * method is called with the first component of this object's
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   950
     * {@code command} array as its argument. This may result in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * a {@link SecurityException} being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * <p>Starting an operating system process is highly system-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * Among the many things that can go wrong are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <li>The operating system program file was not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * <li>Access to the program file was denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <li>The working directory does not exist.
25539
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
   959
     * <li>Invalid character in command argument, such as NUL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * <p>In such cases an exception will be thrown.  The exact nature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * of the exception is system-dependent, but it will always be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * subclass of {@link IOException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
28872
82a09f5a7ed6 8072034: (process) ProcessBuilder.start and Runtime.exec UnsupportedOperationException editorial cleanup
rriggs
parents: 28750
diff changeset
   966
     * <p>If the operating system does not support the creation of
82a09f5a7ed6 8072034: (process) ProcessBuilder.start and Runtime.exec UnsupportedOperationException editorial cleanup
rriggs
parents: 28750
diff changeset
   967
     * processes, an {@link UnsupportedOperationException} will be thrown.
82a09f5a7ed6 8072034: (process) ProcessBuilder.start and Runtime.exec UnsupportedOperationException editorial cleanup
rriggs
parents: 28750
diff changeset
   968
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * <p>Subsequent modifications to this process builder will not
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   970
     * affect the returned {@link Process}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   972
     * @return a new {@link Process} object for managing the subprocess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   973
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   974
     * @throws NullPointerException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   975
     *         if an element of the command list is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   977
     * @throws IndexOutOfBoundsException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   978
     *         if the command is an empty list (has size {@code 0})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   980
     * @throws SecurityException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   981
     *         if a security manager exists and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   982
     *         <ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   983
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   984
     *         <li>its
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   985
     *         {@link SecurityManager#checkExec checkExec}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   986
     *         method doesn't allow creation of the subprocess, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   988
     *         <li>the standard input to the subprocess was
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   989
     *         {@linkplain #redirectInput redirected from a file}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   990
     *         and the security manager's
29600
3c042ba9fd42 8071480: (process spec) ProcessBuilder.start spec linked to the wrong checkRead and checkWrite methods
rriggs
parents: 29381
diff changeset
   991
     *         {@link SecurityManager#checkRead(String) checkRead} method
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   992
     *         denies read access to the file, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   994
     *         <li>the standard output or standard error of the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   995
     *         subprocess was
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   996
     *         {@linkplain #redirectOutput redirected to a file}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   997
     *         and the security manager's
29600
3c042ba9fd42 8071480: (process spec) ProcessBuilder.start spec linked to the wrong checkRead and checkWrite methods
rriggs
parents: 29381
diff changeset
   998
     *         {@link SecurityManager#checkWrite(String) checkWrite} method
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   999
     *         denies write access to the file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1001
     *         </ul>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1002
     *
28750
a1dae439cdab 8055330: (process spec) ProcessBuilder.start and Runtime.exec should throw UnsupportedOperationException on platforms that don't support
rriggs
parents: 28058
diff changeset
  1003
     * @throws  UnsupportedOperationException
a1dae439cdab 8055330: (process spec) ProcessBuilder.start and Runtime.exec should throw UnsupportedOperationException on platforms that don't support
rriggs
parents: 28058
diff changeset
  1004
     *          If the operating system does not support the creation of processes.
a1dae439cdab 8055330: (process spec) ProcessBuilder.start and Runtime.exec should throw UnsupportedOperationException on platforms that don't support
rriggs
parents: 28058
diff changeset
  1005
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1006
     * @throws IOException if an I/O error occurs
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1007
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1008
     * @see Runtime#exec(String[], String[], java.io.File)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public Process start() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        // Must convert to array first -- a malicious user-supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        // list might try to circumvent the security check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        String[] cmdarray = command.toArray(new String[command.size()]);
5186
1bbb82130b38 6910590: Application can modify command array, in ProcessBuilder
michaelm
parents: 48
diff changeset
  1014
        cmdarray = cmdarray.clone();
1bbb82130b38 6910590: Application can modify command array, in ProcessBuilder
michaelm
parents: 48
diff changeset
  1015
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        for (String arg : cmdarray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (arg == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // Throws IndexOutOfBoundsException if command is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        String prog = cmdarray[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (security != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            security.checkExec(prog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        String dir = directory == null ? null : directory.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
25539
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
  1028
        for (int i = 1; i < cmdarray.length; i++) {
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
  1029
            if (cmdarray[i].indexOf('\u0000') >= 0) {
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
  1030
                throw new IOException("invalid null character in command");
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
  1031
            }
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
  1032
        }
9d290547d266 8036571: (process) Process process arguments carefully
rriggs
parents: 23010
diff changeset
  1033
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            return ProcessImpl.start(cmdarray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                                     environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                                     dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1038
                                     redirects,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                                     redirectErrorStream);
17467
374c1cceefff 8012453: (process) Runtime.exec(String) fails if command contains spaces [win]
uta
parents: 16877
diff changeset
  1040
        } catch (IOException | IllegalArgumentException e) {
16877
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1041
            String exceptionInfo = ": " + e.getMessage();
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1042
            Throwable cause = e;
17467
374c1cceefff 8012453: (process) Runtime.exec(String) fails if command contains spaces [win]
uta
parents: 16877
diff changeset
  1043
            if ((e instanceof IOException) && security != null) {
16877
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1044
                // Can not disclose the fail reason for read-protected files.
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1045
                try {
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1046
                    security.checkRead(prog);
19606
6c846d61ba2f 8023717: (process) ProcessBuilder should catch SecurityException rather than AccessControlException
alanb
parents: 18776
diff changeset
  1047
                } catch (SecurityException se) {
16877
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1048
                    exceptionInfo = "";
19606
6c846d61ba2f 8023717: (process) ProcessBuilder should catch SecurityException rather than AccessControlException
alanb
parents: 18776
diff changeset
  1049
                    cause = se;
16877
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1050
                }
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1051
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            // It's much easier for us to create a high-quality error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            // message than the low-level C code which found the problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                "Cannot run program \"" + prog + "\""
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                + (dir == null ? "" : " (in directory \"" + dir + "\")")
16877
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1057
                + exceptionInfo,
d12c06e6e2ba 8005942: (process) Improved Runtime.exec
uta
parents: 14342
diff changeset
  1058
                cause);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
}