./src/share/classes/java/lang/Process.java

Print this page




   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 import java.util.concurrent.TimeUnit;
  30 
  31 /**
  32  * The {@link ProcessBuilder#start()} and
  33  * {@link Runtime#exec(String[],String[],File) Runtime.exec}
  34  * methods create a native process and return an instance of a
  35  * subclass of {@code Process} that can be used to control the process
  36  * and obtain information about it.  The class {@code Process}
  37  * provides methods for performing input from the process, performing
  38  * output to the process, waiting for the process to complete,
  39  * checking the exit status of the process, and destroying (killing)
  40  * the process.
  41  *
  42  * <p>The methods that create processes may not work well for special
  43  * processes on certain native platforms, such as native windowing
  44  * processes, daemon processes, Win16/DOS processes on Microsoft
  45  * Windows, or shell scripts.
  46  *
  47  * <p>By default, the created subprocess does not have its own terminal
  48  * or console.  All its standard I/O (i.e. stdin, stdout, stderr)


  58  * the subprocess may cause the subprocess to block, or even deadlock.
  59  *
  60  * <p>Where desired, <a href="ProcessBuilder.html#redirect-input">
  61  * subprocess I/O can also be redirected</a>
  62  * using methods of the {@link ProcessBuilder} class.
  63  *
  64  * <p>The subprocess is not killed when there are no more references to
  65  * the {@code Process} object, but rather the subprocess
  66  * continues executing asynchronously.
  67  *
  68  * <p>There is no requirement that a process represented by a {@code
  69  * Process} object execute asynchronously or concurrently with respect
  70  * to the Java process that owns the {@code Process} object.
  71  *
  72  * <p>As of 1.5, {@link ProcessBuilder#start()} is the preferred way
  73  * to create a {@code Process}.
  74  *
  75  * @since   JDK1.0
  76  */
  77 public abstract class Process {

  78     /**
  79      * Returns the output stream connected to the normal input of the
  80      * subprocess.  Output to the stream is piped into the standard
  81      * input of the process represented by this {@code Process} object.
  82      *
  83      * <p>If the standard input of the subprocess has been redirected using
  84      * {@link ProcessBuilder#redirectInput(Redirect)
  85      * ProcessBuilder.redirectInput}
  86      * then this method will return a
  87      * <a href="ProcessBuilder.html#redirect-input">null output stream</a>.
  88      *
  89      * <p>Implementation note: It is a good idea for the returned
  90      * output stream to be buffered.
  91      *
  92      * @return the output stream connected to the normal input of the
  93      *         subprocess
  94      */
  95     public abstract OutputStream getOutputStream();
  96 
  97     /**


 243      */
 244     public Process destroyForcibly() {
 245         destroy();
 246         return this;
 247     }
 248 
 249     /**
 250      * Tests whether the subprocess represented by this {@code Process} is
 251      * alive.
 252      *
 253      * @return {@code true} if the subprocess represented by this
 254      *         {@code Process} object has not yet terminated.
 255      * @since 1.8
 256      */
 257     public boolean isAlive() {
 258         try {
 259             exitValue();
 260             return false;
 261         } catch(IllegalThreadStateException e) {
 262             return true;








































 263         }
 264     }
 265 }


   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 import java.util.ArrayList;
  30 import java.util.Collections;
  31 import java.util.List;
  32 import java.util.concurrent.TimeUnit;
  33 
  34 /**
  35  * The {@link ProcessBuilder#start()} and
  36  * {@link Runtime#exec(String[],String[],File) Runtime.exec}
  37  * methods create a native process and return an instance of a
  38  * subclass of {@code Process} that can be used to control the process
  39  * and obtain information about it.  The class {@code Process}
  40  * provides methods for performing input from the process, performing
  41  * output to the process, waiting for the process to complete,
  42  * checking the exit status of the process, and destroying (killing)
  43  * the process.
  44  *
  45  * <p>The methods that create processes may not work well for special
  46  * processes on certain native platforms, such as native windowing
  47  * processes, daemon processes, Win16/DOS processes on Microsoft
  48  * Windows, or shell scripts.
  49  *
  50  * <p>By default, the created subprocess does not have its own terminal
  51  * or console.  All its standard I/O (i.e. stdin, stdout, stderr)


  61  * the subprocess may cause the subprocess to block, or even deadlock.
  62  *
  63  * <p>Where desired, <a href="ProcessBuilder.html#redirect-input">
  64  * subprocess I/O can also be redirected</a>
  65  * using methods of the {@link ProcessBuilder} class.
  66  *
  67  * <p>The subprocess is not killed when there are no more references to
  68  * the {@code Process} object, but rather the subprocess
  69  * continues executing asynchronously.
  70  *
  71  * <p>There is no requirement that a process represented by a {@code
  72  * Process} object execute asynchronously or concurrently with respect
  73  * to the Java process that owns the {@code Process} object.
  74  *
  75  * <p>As of 1.5, {@link ProcessBuilder#start()} is the preferred way
  76  * to create a {@code Process}.
  77  *
  78  * @since   JDK1.0
  79  */
  80 public abstract class Process {
  81 
  82     /**
  83      * Returns the output stream connected to the normal input of the
  84      * subprocess.  Output to the stream is piped into the standard
  85      * input of the process represented by this {@code Process} object.
  86      *
  87      * <p>If the standard input of the subprocess has been redirected using
  88      * {@link ProcessBuilder#redirectInput(Redirect)
  89      * ProcessBuilder.redirectInput}
  90      * then this method will return a
  91      * <a href="ProcessBuilder.html#redirect-input">null output stream</a>.
  92      *
  93      * <p>Implementation note: It is a good idea for the returned
  94      * output stream to be buffered.
  95      *
  96      * @return the output stream connected to the normal input of the
  97      *         subprocess
  98      */
  99     public abstract OutputStream getOutputStream();
 100 
 101     /**


 247      */
 248     public Process destroyForcibly() {
 249         destroy();
 250         return this;
 251     }
 252 
 253     /**
 254      * Tests whether the subprocess represented by this {@code Process} is
 255      * alive.
 256      *
 257      * @return {@code true} if the subprocess represented by this
 258      *         {@code Process} object has not yet terminated.
 259      * @since 1.8
 260      */
 261     public boolean isAlive() {
 262         try {
 263             exitValue();
 264             return false;
 265         } catch(IllegalThreadStateException e) {
 266             return true;
 267         }
 268     }
 269     
 270     /**
 271      * Returns the process id of the subprocess
 272      * 
 273      * @return the process id or -1 if the process is not alive
 274      * @since 1.8
 275      */
 276     public abstract int getPid();
 277 
 278     /**
 279      * Returns the process id of the current process.
 280      * 
 281      * @return the process id of the current process.
 282      * @since 1.8
 283      *
 284      **/
 285     public static native int getCurrentPid();
 286     
 287     public abstract String getProcessName(int pid);
 288 
 289     /**
 290      * @return the process name of the current process.
 291      * @since 1.8
 292      */
 293     public String getCurrentProcessName() {
 294         return getProcessName( getCurrentPid() );
 295     }
 296 
 297     /**
 298      * @return the process name of this process or null if no longer live
 299      * @since 1.8
 300      */
 301     public String getProcessName() {
 302         int pid = getPid();
 303         if (pid >=0) {
 304             return getProcessName( getPid() );
 305         } else {
 306             return null;
 307         }
 308     }
 309 }