• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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  *
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.
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
44  * Windows, or shell scripts.
45  *
46  * <p>By default, the created subprocess does not have its own terminal
47  * or console.  All its standard I/O (i.e. stdin, stdout, stderr)
48  * operations will be redirected to the parent process, where they can
49  * be accessed via the streams obtained using the methods
50  * {@link #getOutputStream()},
51  * {@link #getInputStream()}, and
52  * {@link #getErrorStream()}.
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
57  * the subprocess may cause the subprocess to block, or even deadlock.
58  *
59  * <p>The subprocess is not killed when there are no more references to
60  * the {@code Process} object, but rather the subprocess
61  * continues executing asynchronously.
62  *
63  * <p>There is no requirement that a process represented by a {@code
64  * Process} object execute asynchronously or concurrently with respect
65  * to the Java process that owns the {@code Process} object.
66  *
67  * <p>As of 1.5, {@link ProcessBuilder#start()} is the preferred way
68  * to create a {@code Process}.
69  *
70  * @since   JDK1.0
71  */
72 public abstract class Process {
73     /**
74      * Returns the output stream connected to the normal input of the
75      * subprocess.  Output to the stream is piped into the standard
76      * input of the process represented by this {@code Process} object.
77      *
78      * <p>Implementation note: It is a good idea for the returned
79      * output stream to be buffered.
80      *
81      * @return the output stream connected to the normal input of the
82      *         subprocess
83      */
getOutputStream()84     abstract public OutputStream getOutputStream();
85 
86     /**
87      * Returns the input stream connected to the normal output of the
88      * subprocess.  The stream obtains data piped from the standard
89      * output of the process represented by this {@code Process} object.
90      *
91      * <p>Implementation note: It is a good idea for the returned
92      * input stream to be buffered.
93      *
94      * @return the input stream connected to the normal output of the
95      *         subprocess
96      */
getInputStream()97     abstract public InputStream getInputStream();
98 
99     /**
100      * Returns the input stream connected to the error output of the
101      * subprocess.  The stream obtains data piped from the error output
102      * of the process represented by this {@code Process} object.
103      *
104      * <p>Implementation note: It is a good idea for the returned
105      * input stream to be buffered.
106      *
107      * @return the input stream connected to the error output of
108      *         the subprocess
109      */
getErrorStream()110     abstract public InputStream getErrorStream();
111 
112     /**
113      * Causes the current thread to wait, if necessary, until the
114      * process represented by this {@code Process} object has
115      * terminated.  This method returns immediately if the subprocess
116      * has already terminated.  If the subprocess has not yet
117      * terminated, the calling thread will be blocked until the
118      * subprocess exits.
119      *
120      * @return the exit value of the subprocess represented by this
121      *         {@code Process} object.  By convention, the value
122      *         {@code 0} indicates normal termination.
123      * @throws InterruptedException if the current thread is
124      *         {@linkplain Thread#interrupt() interrupted} by another
125      *         thread while it is waiting, then the wait is ended and
126      *         an {@link InterruptedException} is thrown.
127      */
waitFor()128     abstract public int waitFor() throws InterruptedException;
129 
130     /**
131      * Returns the exit value for the subprocess.
132      *
133      * @return the exit value of the subprocess represented by this
134      *         {@code Process} object.  By convention, the value
135      *         {@code 0} indicates normal termination.
136      * @throws IllegalThreadStateException if the subprocess represented
137      *         by this {@code Process} object has not yet terminated
138      */
exitValue()139     abstract public int exitValue();
140 
141     /**
142      * Kills the subprocess. The subprocess represented by this
143      * {@code Process} object is forcibly terminated.
144      */
destroy()145     abstract public void destroy();
146 }
147