author | simonis |
Fri, 14 Aug 2015 10:35:45 +0200 | |
changeset 32209 | 24bb680a1609 |
parent 29601 | d6e9fbd8497a |
child 32649 | 2ee9017c7597 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
28 |
import java.io.File; |
|
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 | 33 |
import java.util.ArrayList; |
34 |
import java.util.List; |
|
35 |
import java.util.Map; |
|
36 |
||
37 |
/** |
|
38 |
* This class is used to create operating system processes. |
|
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 | 41 |
* of process attributes. The {@link #start()} method creates a new |
42 |
* {@link Process} instance with those attributes. The {@link |
|
43 |
* #start()} method can be invoked repeatedly from the same instance |
|
44 |
* to create new subprocesses with identical or related attributes. |
|
45 |
* |
|
46 |
* <p>Each process builder manages these process attributes: |
|
47 |
* |
|
48 |
* <ul> |
|
49 |
* |
|
50 |
* <li>a <i>command</i>, a list of strings which signifies the |
|
51 |
* external program file to be invoked and its arguments, if any. |
|
52 |
* Which string lists represent a valid operating system command is |
|
53 |
* system-dependent. For example, it is common for each conceptual |
|
54 |
* argument to be an element in this list, but there are operating |
|
55 |
* systems where programs are expected to tokenize command line |
|
56 |
* strings themselves - on such a system a Java implementation might |
|
57 |
* require commands to contain exactly two elements. |
|
58 |
* |
|
59 |
* <li>an <i>environment</i>, which is a system-dependent mapping from |
|
60 |
* <i>variables</i> to <i>values</i>. The initial value is a copy of |
|
61 |
* the environment of the current process (see {@link System#getenv()}). |
|
62 |
* |
|
63 |
* <li>a <i>working directory</i>. The default value is the current |
|
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 | 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 | 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 | 101 |
* |
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 | 104 |
* output of a subprocess are sent to two separate streams, which can |
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 | 123 |
* |
124 |
* </ul> |
|
125 |
* |
|
126 |
* <p>Modifying a process builder's attributes will affect processes |
|
127 |
* subsequently started by that object's {@link #start()} method, but |
|
128 |
* will never affect previously started processes or the Java process |
|
129 |
* itself. |
|
130 |
* |
|
131 |
* <p>Most error checking is performed by the {@link #start()} method. |
|
132 |
* It is possible to modify the state of an object so that {@link |
|
133 |
* #start()} will fail. For example, setting the command attribute to |
|
134 |
* an empty list will not throw an exception unless {@link #start()} |
|
135 |
* is invoked. |
|
136 |
* |
|
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 | 139 |
* concurrently, and at least one of the threads modifies one of the |
140 |
* attributes structurally, it <i>must</i> be synchronized externally. |
|
141 |
* |
|
142 |
* <p>Starting a new process which uses the default working directory |
|
143 |
* and environment is easy: |
|
144 |
* |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
145 |
* <pre> {@code |
2 | 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 | 148 |
* |
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 | 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 | 157 |
* env.put("VAR1", "myValue"); |
158 |
* env.remove("OTHERVAR"); |
|
159 |
* env.put("VAR2", env.get("VAR1") + "suffix"); |
|
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 | 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 | 169 |
* |
170 |
* <p>To start a process with an explicit set of environment |
|
171 |
* variables, first call {@link java.util.Map#clear() Map.clear()} |
|
172 |
* before adding environment variables. |
|
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 | 180 |
* @since 1.5 |
181 |
*/ |
|
182 |
||
183 |
public final class ProcessBuilder |
|
184 |
{ |
|
185 |
private List<String> command; |
|
186 |
private File directory; |
|
187 |
private Map<String,String> environment; |
|
188 |
private boolean redirectErrorStream; |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
189 |
private Redirect[] redirects; |
2 | 190 |
|
191 |
/** |
|
192 |
* Constructs a process builder with the specified operating |
|
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 | 195 |
* updates to the list will be reflected in the state of the |
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 | 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 | 201 |
*/ |
202 |
public ProcessBuilder(List<String> command) { |
|
203 |
if (command == null) |
|
204 |
throw new NullPointerException(); |
|
205 |
this.command = command; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Constructs a process builder with the specified operating |
|
210 |
* system program and arguments. This is a convenience |
|
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 | 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 | 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 | 218 |
*/ |
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 | 221 |
for (String arg : command) |
222 |
this.command.add(arg); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Sets this process builder's operating system program and |
|
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 | 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 | 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 | 235 |
*/ |
236 |
public ProcessBuilder command(List<String> command) { |
|
237 |
if (command == null) |
|
238 |
throw new NullPointerException(); |
|
239 |
this.command = command; |
|
240 |
return this; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Sets this process builder's operating system program and |
|
245 |
* arguments. This is a convenience method that sets the command |
|
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 | 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 | 253 |
*/ |
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 | 256 |
for (String arg : command) |
257 |
this.command.add(arg); |
|
258 |
return this; |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Returns this process builder's operating system program and |
|
263 |
* arguments. The returned list is <i>not</i> a copy. Subsequent |
|
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 | 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 | 268 |
*/ |
269 |
public List<String> command() { |
|
270 |
return command; |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Returns a string map view of this process builder's environment. |
|
275 |
* |
|
276 |
* Whenever a process builder is created, the environment is |
|
277 |
* initialized to a copy of the current process environment (see |
|
278 |
* {@link System#getenv()}). Subprocesses subsequently started by |
|
279 |
* this object's {@link #start()} method will use this map as |
|
280 |
* their environment. |
|
281 |
* |
|
282 |
* <p>The returned object may be modified using ordinary {@link |
|
283 |
* java.util.Map Map} operations. These modifications will be |
|
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 | 286 |
* contain independent process environments, so changes to the |
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 | 289 |
* {@link System#getenv System.getenv}. |
290 |
* |
|
291 |
* <p>If the system does not support environment variables, an |
|
292 |
* empty map is returned. |
|
293 |
* |
|
294 |
* <p>The returned map does not permit null keys or values. |
|
295 |
* Attempting to insert or query the presence of a null key or |
|
296 |
* value will throw a {@link NullPointerException}. |
|
297 |
* Attempting to query the presence of a key or value which is not |
|
298 |
* of type {@link String} will throw a {@link ClassCastException}. |
|
299 |
* |
|
300 |
* <p>The behavior of the returned map is system-dependent. A |
|
301 |
* system may not allow modifications to environment variables or |
|
302 |
* may forbid certain variable names or values. For this reason, |
|
303 |
* attempts to modify the map may fail with |
|
304 |
* {@link UnsupportedOperationException} or |
|
305 |
* {@link IllegalArgumentException} |
|
306 |
* if the modification is not permitted by the operating system. |
|
307 |
* |
|
308 |
* <p>Since the external format of environment variable names and |
|
309 |
* values is system-dependent, there may not be a one-to-one |
|
310 |
* mapping between them and Java's Unicode strings. Nevertheless, |
|
311 |
* the map is implemented in such a way that environment variables |
|
312 |
* which are not modified by Java code will have an unmodified |
|
313 |
* native representation in the subprocess. |
|
314 |
* |
|
315 |
* <p>The returned map and its collection views may not obey the |
|
316 |
* general contract of the {@link Object#equals} and |
|
317 |
* {@link Object#hashCode} methods. |
|
318 |
* |
|
319 |
* <p>The returned map is typically case-sensitive on all platforms. |
|
320 |
* |
|
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 | 326 |
* |
327 |
* <p>When passing information to a Java subprocess, |
|
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 | 330 |
* |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
331 |
* @return this process builder's environment |
2 | 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 | 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 | 340 |
*/ |
341 |
public Map<String,String> environment() { |
|
342 |
SecurityManager security = System.getSecurityManager(); |
|
343 |
if (security != null) |
|
344 |
security.checkPermission(new RuntimePermission("getenv.*")); |
|
345 |
||
346 |
if (environment == null) |
|
347 |
environment = ProcessEnvironment.environment(); |
|
348 |
||
349 |
assert environment != null; |
|
350 |
||
351 |
return environment; |
|
352 |
} |
|
353 |
||
354 |
// Only for use by Runtime.exec(...envp...) |
|
355 |
ProcessBuilder environment(String[] envp) { |
|
356 |
assert environment == null; |
|
357 |
if (envp != null) { |
|
358 |
environment = ProcessEnvironment.emptyEnvironment(envp.length); |
|
359 |
assert environment != null; |
|
360 |
||
361 |
for (String envstring : envp) { |
|
362 |
// Before 1.5, we blindly passed invalid envstrings |
|
363 |
// to the child process. |
|
364 |
// We would like to throw an exception, but do not, |
|
365 |
// for compatibility with old broken code. |
|
366 |
||
367 |
// Silently discard any trailing junk. |
|
368 |
if (envstring.indexOf((int) '\u0000') != -1) |
|
369 |
envstring = envstring.replaceFirst("\u0000.*", ""); |
|
370 |
||
371 |
int eqlsign = |
|
372 |
envstring.indexOf('=', ProcessEnvironment.MIN_NAME_LENGTH); |
|
373 |
// Silently ignore envstrings lacking the required `='. |
|
374 |
if (eqlsign != -1) |
|
375 |
environment.put(envstring.substring(0,eqlsign), |
|
376 |
envstring.substring(eqlsign+1)); |
|
377 |
} |
|
378 |
} |
|
379 |
return this; |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* Returns this process builder's working directory. |
|
384 |
* |
|
385 |
* Subprocesses subsequently started by this object's {@link |
|
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 | 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 | 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 | 393 |
*/ |
394 |
public File directory() { |
|
395 |
return directory; |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Sets this process builder's working directory. |
|
400 |
* |
|
401 |
* Subprocesses subsequently started by this object's {@link |
|
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 | 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 | 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 | 410 |
*/ |
411 |
public ProcessBuilder directory(File directory) { |
|
412 |
this.directory = directory; |
|
413 |
return this; |
|
414 |
} |
|
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 | 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 | 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 | 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 | 892 |
/** |
893 |
* Tells whether this process builder merges standard error and |
|
894 |
* standard output. |
|
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 | 897 |
* generated by subprocesses subsequently started by this object's |
898 |
* {@link #start()} method will be merged with the standard |
|
899 |
* output, so that both can be read using the |
|
900 |
* {@link Process#getInputStream()} method. This makes it easier |
|
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 | 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 | 905 |
*/ |
906 |
public boolean redirectErrorStream() { |
|
907 |
return redirectErrorStream; |
|
908 |
} |
|
909 |
||
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 | 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 | 914 |
* generated by subprocesses subsequently started by this object's |
915 |
* {@link #start()} method will be merged with the standard |
|
916 |
* output, so that both can be read using the |
|
917 |
* {@link Process#getInputStream()} method. This makes it easier |
|
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 | 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 | 923 |
*/ |
924 |
public ProcessBuilder redirectErrorStream(boolean redirectErrorStream) { |
|
925 |
this.redirectErrorStream = redirectErrorStream; |
|
926 |
return this; |
|
927 |
} |
|
928 |
||
929 |
/** |
|
930 |
* Starts a new process using the attributes of this process builder. |
|
931 |
* |
|
932 |
* <p>The new process will |
|
933 |
* invoke the command and arguments given by {@link #command()}, |
|
934 |
* in a working directory as given by {@link #directory()}, |
|
935 |
* with a process environment as given by {@link #environment()}. |
|
936 |
* |
|
937 |
* <p>This method checks that the command is a valid operating |
|
938 |
* system command. Which commands are valid is system-dependent, |
|
939 |
* but at the very least the command must be a non-empty list of |
|
940 |
* non-null strings. |
|
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 | 947 |
* <p>If there is a security manager, its |
948 |
* {@link SecurityManager#checkExec checkExec} |
|
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 | 951 |
* a {@link SecurityException} being thrown. |
952 |
* |
|
953 |
* <p>Starting an operating system process is highly system-dependent. |
|
954 |
* Among the many things that can go wrong are: |
|
955 |
* <ul> |
|
956 |
* <li>The operating system program file was not found. |
|
957 |
* <li>Access to the program file was denied. |
|
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 | 960 |
* </ul> |
961 |
* |
|
962 |
* <p>In such cases an exception will be thrown. The exact nature |
|
963 |
* of the exception is system-dependent, but it will always be a |
|
964 |
* subclass of {@link IOException}. |
|
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 1009 |
*/ |
1010 |
public Process start() throws IOException { |
|
1011 |
// Must convert to array first -- a malicious user-supplied |
|
1012 |
// list might try to circumvent the security check. |
|
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 | 1016 |
for (String arg : cmdarray) |
1017 |
if (arg == null) |
|
1018 |
throw new NullPointerException(); |
|
1019 |
// Throws IndexOutOfBoundsException if command is empty |
|
1020 |
String prog = cmdarray[0]; |
|
1021 |
||
1022 |
SecurityManager security = System.getSecurityManager(); |
|
1023 |
if (security != null) |
|
1024 |
security.checkExec(prog); |
|
1025 |
||
1026 |
String dir = directory == null ? null : directory.toString(); |
|
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 | 1034 |
try { |
1035 |
return ProcessImpl.start(cmdarray, |
|
1036 |
environment, |
|
1037 |
dir, |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
1038 |
redirects, |
2 | 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 | 1041 |
String exceptionInfo = ": " + e.getMessage(); |
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 | 1044 |
// Can not disclose the fail reason for read-protected files. |
1045 |
try { |
|
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 | 1048 |
exceptionInfo = ""; |
19606
6c846d61ba2f
8023717: (process) ProcessBuilder should catch SecurityException rather than AccessControlException
alanb
parents:
18776
diff
changeset
|
1049 |
cause = se; |
16877 | 1050 |
} |
1051 |
} |
|
2 | 1052 |
// It's much easier for us to create a high-quality error |
1053 |
// message than the low-level C code which found the problem. |
|
1054 |
throw new IOException( |
|
1055 |
"Cannot run program \"" + prog + "\"" |
|
1056 |
+ (dir == null ? "" : " (in directory \"" + dir + "\")") |
|
16877 | 1057 |
+ exceptionInfo, |
1058 |
cause); |
|
2 | 1059 |
} |
1060 |
} |
|
1061 |
} |