Android Guides | Samples

Java.Lang.Process Class

Represents an external process.

See Also: Process

Syntax

[Android.Runtime.Register("java/lang/Process", DoNotGenerateAcw=true)]
public abstract class Process : Object

Remarks

Represents an external process. Enables writing to, reading from, destroying, and waiting for the external process, as well as querying its exit value. Use ProcessBuilder to create processes.

The child process writes its output to two streams, out and err. These streams should be read by the parent process using Process.InputStream and Process.ErrorStream respectively. If these streams are not read, the target process may block while it awaits buffer space. It isn't sufficient to read the streams in sequence; to avoid blocking each of the two streams must have its own reader thread. If you are not interested in differentiating the out and err streams, use ProcessBuilder.RedirectErrorStream(Boolean) to merge the two streams. This simplifies your reading code and makes it easier to avoid blocking the target process.

Running processes hold resources. When a process is no longer used, the process should be closed by calling Process.Destroy. This will kill the process and release the resources that it holds.

For example, to run /system/bin/ping to ping android.com:

java Example

Process process = new ProcessBuilder()
       .command("/system/bin/ping", "android.com")
       .redirectErrorStream(true)
       .start();
   try {
     InputStream in = process.getInputStream();
     OutputStream out = process.getOutputStream();

     readStream(in);

    finally {
     process.destroy();
   }
 }

[Android Documentation]

Requirements

Namespace: Java.Lang
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Java.Lang.Process are listed below.

See Also: Object

Public Constructors

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Properties

[read-only]
abstract
ErrorStreamStream. Returns an input stream that is connected to the error stream (stderr) of the native process represented by this object.
[read-only]
abstract
InputStreamStream. Returns an input stream that is connected to the standard output stream (stdout) of the native process represented by this object.
[read-only]
abstract
OutputStreamStream. Returns an output stream that is connected to the standard input stream (stdin) of the native process represented by this object.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

abstract
Destroy()
Terminates this process and closes any associated streams.
abstract
ExitValue() : Int32
Returns the exit value of the native process represented by this object.
abstract
WaitFor() : Int32
Causes the calling thread to wait for the native process associated with this object to finish executing.
WaitForAsync() : System.Threading.Tasks.Task<int>