author | jrose |
Tue, 17 May 2011 19:48:14 -0700 | |
changeset 9730 | e4b334d47f4b |
parent 5506 | 202f599c92aa |
child 13149 | 27d52f97a5cc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1995, 2008, 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.*; |
|
29 |
||
30 |
/** |
|
31 |
* The {@link ProcessBuilder#start()} and |
|
32 |
* {@link Runtime#exec(String[],String[],File) Runtime.exec} |
|
33 |
* methods create a native process and return an instance of a |
|
34 |
* subclass of {@code Process} that can be used to control the process |
|
35 |
* and obtain information about it. The class {@code Process} |
|
36 |
* provides methods for performing input from the process, performing |
|
37 |
* output to the process, waiting for the process to complete, |
|
38 |
* checking the exit status of the process, and destroying (killing) |
|
39 |
* the process. |
|
40 |
* |
|
41 |
* <p>The methods that create processes may not work well for special |
|
42 |
* processes on certain native platforms, such as native windowing |
|
43 |
* processes, daemon processes, Win16/DOS processes on Microsoft |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
44 |
* Windows, or shell scripts. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
45 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
46 |
* <p>By default, the created subprocess does not have its own terminal |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
47 |
* or console. All its standard I/O (i.e. stdin, stdout, stderr) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
48 |
* operations will be redirected to the parent process, where they can |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
49 |
* be accessed via the streams obtained using the methods |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
50 |
* {@link #getOutputStream()}, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
51 |
* {@link #getInputStream()}, and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
52 |
* {@link #getErrorStream()}. |
2 | 53 |
* The parent process uses these streams to feed input to and get output |
54 |
* from the subprocess. Because some native platforms only provide |
|
55 |
* limited buffer size for standard input and output streams, failure |
|
56 |
* to promptly write the input stream or read the output stream of |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
57 |
* the subprocess may cause the subprocess to block, or even deadlock. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
58 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
59 |
* <p>Where desired, <a href="ProcessBuilder.html#redirect-input"> |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
60 |
* subprocess I/O can also be redirected</a> |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
61 |
* using methods of the {@link ProcessBuilder} class. |
2 | 62 |
* |
63 |
* <p>The subprocess is not killed when there are no more references to |
|
64 |
* the {@code Process} object, but rather the subprocess |
|
65 |
* continues executing asynchronously. |
|
66 |
* |
|
67 |
* <p>There is no requirement that a process represented by a {@code |
|
68 |
* Process} object execute asynchronously or concurrently with respect |
|
69 |
* to the Java process that owns the {@code Process} object. |
|
70 |
* |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
71 |
* <p>As of 1.5, {@link ProcessBuilder#start()} is the preferred way |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
72 |
* to create a {@code Process}. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
73 |
* |
2 | 74 |
* @since JDK1.0 |
75 |
*/ |
|
76 |
public abstract class Process { |
|
77 |
/** |
|
78 |
* Returns the output stream connected to the normal input of the |
|
79 |
* subprocess. Output to the stream is piped into the standard |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
80 |
* input of the process represented by this {@code Process} object. |
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 |
* <p>If the standard input of the subprocess has been redirected using |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
83 |
* {@link ProcessBuilder#redirectInput(Redirect) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
84 |
* ProcessBuilder.redirectInput} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
85 |
* then this method will return a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
86 |
* <a href="ProcessBuilder.html#redirect-input">null output stream</a>. |
2 | 87 |
* |
88 |
* <p>Implementation note: It is a good idea for the returned |
|
89 |
* output stream to be buffered. |
|
90 |
* |
|
91 |
* @return the output stream connected to the normal input of the |
|
92 |
* subprocess |
|
93 |
*/ |
|
94 |
abstract public OutputStream getOutputStream(); |
|
95 |
||
96 |
/** |
|
97 |
* Returns the input stream connected to the normal output of the |
|
98 |
* subprocess. The stream obtains data piped from the standard |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
99 |
* output of the process represented by this {@code Process} object. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
100 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
101 |
* <p>If the standard output of the subprocess has been redirected using |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
102 |
* {@link ProcessBuilder#redirectOutput(Redirect) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
103 |
* ProcessBuilder.redirectOutput} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
104 |
* then this method will return a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
105 |
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
106 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
107 |
* <p>Otherwise, if the standard error of the subprocess has been |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
108 |
* redirected using |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
109 |
* {@link ProcessBuilder#redirectErrorStream(boolean) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
110 |
* ProcessBuilder.redirectErrorStream} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
111 |
* then the input stream returned by this method will receive the |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
112 |
* merged standard output and the standard error of the subprocess. |
2 | 113 |
* |
114 |
* <p>Implementation note: It is a good idea for the returned |
|
115 |
* input stream to be buffered. |
|
116 |
* |
|
117 |
* @return the input stream connected to the normal output of the |
|
118 |
* subprocess |
|
119 |
*/ |
|
120 |
abstract public InputStream getInputStream(); |
|
121 |
||
122 |
/** |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
123 |
* Returns the input stream connected to the error output of the |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
124 |
* subprocess. The stream obtains data piped from the error output |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
125 |
* of the process represented by this {@code Process} object. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
126 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
127 |
* <p>If the standard error of the subprocess has been redirected using |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
128 |
* {@link ProcessBuilder#redirectError(Redirect) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
129 |
* ProcessBuilder.redirectError} or |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
130 |
* {@link ProcessBuilder#redirectErrorStream(boolean) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
131 |
* ProcessBuilder.redirectErrorStream} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
132 |
* then this method will return a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
133 |
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>. |
2 | 134 |
* |
135 |
* <p>Implementation note: It is a good idea for the returned |
|
136 |
* input stream to be buffered. |
|
137 |
* |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
138 |
* @return the input stream connected to the error output of |
2 | 139 |
* the subprocess |
140 |
*/ |
|
141 |
abstract public InputStream getErrorStream(); |
|
142 |
||
143 |
/** |
|
144 |
* Causes the current thread to wait, if necessary, until the |
|
145 |
* process represented by this {@code Process} object has |
|
146 |
* terminated. This method returns immediately if the subprocess |
|
147 |
* has already terminated. If the subprocess has not yet |
|
148 |
* terminated, the calling thread will be blocked until the |
|
149 |
* subprocess exits. |
|
150 |
* |
|
151 |
* @return the exit value of the subprocess represented by this |
|
152 |
* {@code Process} object. By convention, the value |
|
153 |
* {@code 0} indicates normal termination. |
|
154 |
* @throws InterruptedException if the current thread is |
|
155 |
* {@linkplain Thread#interrupt() interrupted} by another |
|
156 |
* thread while it is waiting, then the wait is ended and |
|
157 |
* an {@link InterruptedException} is thrown. |
|
158 |
*/ |
|
159 |
abstract public int waitFor() throws InterruptedException; |
|
160 |
||
161 |
/** |
|
162 |
* Returns the exit value for the subprocess. |
|
163 |
* |
|
164 |
* @return the exit value of the subprocess represented by this |
|
165 |
* {@code Process} object. By convention, the value |
|
166 |
* {@code 0} indicates normal termination. |
|
167 |
* @throws IllegalThreadStateException if the subprocess represented |
|
168 |
* by this {@code Process} object has not yet terminated |
|
169 |
*/ |
|
170 |
abstract public int exitValue(); |
|
171 |
||
172 |
/** |
|
173 |
* Kills the subprocess. The subprocess represented by this |
|
174 |
* {@code Process} object is forcibly terminated. |
|
175 |
*/ |
|
176 |
abstract public void destroy(); |
|
177 |
} |