共用方式為


ProcessBuilder Class

Definition

This class is used to create operating system processes.

[Android.Runtime.Register("java/lang/ProcessBuilder", DoNotGenerateAcw=true)]
public sealed class ProcessBuilder : Java.Lang.Object
[<Android.Runtime.Register("java/lang/ProcessBuilder", DoNotGenerateAcw=true)>]
type ProcessBuilder = class
    inherit Object
Inheritance
ProcessBuilder
Attributes

Remarks

This class is used to create operating system processes.

Each ProcessBuilder instance manages a collection of process attributes. The #start() method creates a new Process instance with those attributes. The #start() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes.

Each process builder manages these process attributes:

<ul>

<li>a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. For example, it is common for each conceptual argument to be an element in this list, but there are operating systems where programs are expected to tokenize command line strings themselves - on such a system a Java implementation might require commands to contain exactly two elements.

<li>an environment, which is a system-dependent mapping from variables to values. The initial value is a copy of the environment of the current process (see System#getenv()).

<li>a working directory. The default value is the current working directory of the current process, usually the directory named by the system property user.dir.

<li>"redirect-input">a source of <i>standard input</i>. By default, the subprocess reads input from a pipe. Java code can access this pipe via the output stream returned by Process#getOutputStream(). However, standard input may be redirected to another source using #redirectInput(Redirect) redirectInput. In this case, Process#getOutputStream() will return a null output stream, for which:

<ul> <li>the OutputStream#write(int) write methods always throw IOException<li>the OutputStream#close() close method does nothing </ul>

<li>"redirect-output">a destination for <i>standard output</i> and <i>standard error</i>. By default, the subprocess writes standard output and standard error to pipes. Java code can access these pipes via the input streams returned by Process#getInputStream() and Process#getErrorStream(). However, standard output and standard error may be redirected to other destinations using #redirectOutput(Redirect) redirectOutput and #redirectError(Redirect) redirectError. In this case, Process#getInputStream() and/or Process#getErrorStream() will return a null input stream, for which:

<ul> <li>the InputStream#read() read methods always return -1<li>the InputStream#available() available method always returns 0<li>the InputStream#close() close method does nothing </ul>

<li>a redirectErrorStream property. Initially, this property is false, meaning that the standard output and error output of a subprocess are sent to two separate streams, which can be accessed using the Process#getInputStream() and Process#getErrorStream() methods.

If the value is set to true, then:

<ul> <li>standard error is merged with the standard output and always sent to the same destination (this makes it easier to correlate error messages with the corresponding output) <li>the common destination of standard error and standard output can be redirected using #redirectOutput(Redirect) redirectOutput<li>any redirection set by the #redirectError(Redirect) redirectError method is ignored when creating a subprocess <li>the stream returned from Process#getErrorStream() will always be a null input stream</ul>

</ul>

Modifying a process builder's attributes will affect processes subsequently started by that object's #start() method, but will never affect previously started processes or the Java process itself.

Most error checking is performed by the #start() method. It is possible to modify the state of an object so that #start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless #start() is invoked.

<strong>Note that this class is not synchronized.</strong> If multiple threads access a ProcessBuilder instance concurrently, and at least one of the threads modifies one of the attributes structurally, it must be synchronized externally.

Starting a new process which uses the default working directory and environment is easy:

{@code
            Process p = new ProcessBuilder("myCommand", "myArg").start();
            }

Here is an example that starts a process with a modified working directory and environment, and redirects standard output and error to be appended to a log file:

{@code
            ProcessBuilder pb =
              new ProcessBuilder("myCommand", "myArg1", "myArg2");
            Map<String, String> env = pb.environment();
            env.put("VAR1", "myValue");
            env.remove("OTHERVAR");
            env.put("VAR2", env.get("VAR1") + "suffix");
            pb.directory(new File("myDir"));
            File log = new File("log");
            pb.redirectErrorStream(true);
            pb.redirectOutput(Redirect.appendTo(log));
            Process p = pb.start();
            assert pb.redirectInput() == Redirect.PIPE;
            assert pb.redirectOutput().file() == log;
            assert p.getInputStream().read() == -1;
            }

To start a process with an explicit set of environment variables, first call java.util.Map#clear() Map.clear() before adding environment variables.

Added in 1.5.

Java documentation for java.lang.ProcessBuilder.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ProcessBuilder(IList<String>)

Constructs a process builder with the specified operating system program and arguments.

ProcessBuilder(String[])

Constructs a process builder with the specified operating system program and arguments.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Command()

Returns this process builder's operating system program and arguments.

Command(IList<String>)

Sets this process builder's operating system program and arguments.

Command(String[])

Returns this process builder's operating system program and arguments.

Directory()

Returns this process builder's working directory.

Directory(File)

Sets this process builder's working directory.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Environment()

Returns a string map view of this process builder's environment.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
InheritIO()

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
RedirectError()

Returns this process builder's standard error destination.

RedirectError(File)

Sets this process builder's standard error destination to a file.

RedirectError(ProcessBuilder+Redirect)

Returns this process builder's standard error destination.

RedirectErrorStream()

Tells whether this process builder merges standard error and standard output.

RedirectErrorStream(Boolean)

Sets this process builder's redirectErrorStream property.

RedirectInput()

Returns this process builder's standard input source.

RedirectInput(File)

Sets this process builder's standard input source to a file.

RedirectInput(ProcessBuilder+Redirect)

Returns this process builder's standard input source.

RedirectOutput()

Returns this process builder's standard output destination.

RedirectOutput(File)

Sets this process builder's standard output destination to a file.

RedirectOutput(ProcessBuilder+Redirect)

Returns this process builder's standard output destination.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Start()

Starts a new process using the attributes of this process builder.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to