FileDocCategorySizeDatePackage
ProcessBuilder.javaAPI DocAndroid 1.5 API7908Wed May 06 22:41:04 BST 2009java.lang

ProcessBuilder

public final class ProcessBuilder extends Object
Creates operating system processes.
since
Android 1.0

Fields Summary
private List
command
private File
directory
private Map
environment
private boolean
redirectErrorStream
Constructors Summary
public ProcessBuilder(String command)
Constructs a new {@code ProcessBuilder} instance with the specified operating system program and its arguments.

param
command the requested operating system program and its arguments.
since
Android 1.0

        this(toList(command));
    
public ProcessBuilder(List command)
Constructs a new {@code ProcessBuilder} instance with the specified operating system program and its arguments. Note that the list passed to this constructor is not copied, so any subsequent updates to it are reflected in this instance's state.

param
command the requested operating system program and its arguments.
throws
NullPointerException if {@code command} is {@code null}.
since
Android 1.0

        super();
        if (command == null) {
            throw new NullPointerException();
        }
        this.command = command;
        // BEGIN android-changed
        this.environment = System.getenv();
        // END android-changed
    
Methods Summary
public java.util.Listcommand()
Returns this process builder's current program and arguments. Note that the returned list is not a copy and modifications to it will change the state of this instance.

return
this process builder's program and arguments.
since
Android 1.0

        return command;
    
public java.lang.ProcessBuildercommand(java.lang.String command)
Changes the program and arguments of this process builder.

param
command the new operating system program and its arguments.
return
this process builder instance.
since
Android 1.0

        return command(toList(command));
    
public java.lang.ProcessBuildercommand(java.util.List command)
Changes the program and arguments of this process builder. Note that the list passed to this method is not copied, so any subsequent updates to it are reflected in this instance's state.

param
command the new operating system program and its arguments.
return
this process builder instance.
throws
NullPointerException if {@code command} is {@code null}.
since
Android 1.0

        if (command == null) {
            throw new NullPointerException();
        }
        this.command = command;
        return this;
    
public java.io.Filedirectory()
Returns the working directory of this process builder. If {@code null} is returned, then the working directory of the Java process is used when a process is started.

return
the current working directory, may be {@code null}.
since
Android 1.0

        return directory;
    
public java.lang.ProcessBuilderdirectory(java.io.File directory)
Changes the working directory of this process builder. If the specified directory is {@code null}, then the working directory of the Java process is used when a process is started.

param
directory the new working directory for this process builder.
return
this process builder instance.
since
Android 1.0

        this.directory = directory;
        return this;
    
public java.util.Mapenvironment()
Returns this process builder's current environment. When a process builder instance is created, the environment is populated with a copy of the environment, as returned by {@link System#getenv()}. Note that the map returned by this method is not a copy and any changes made to it are reflected in this instance's state.

return
the map containing this process builder's environment variables.
since
Android 1.0

        return environment;
    
public java.lang.ProcessBuilderredirectErrorStream(boolean redirectErrorStream)
Changes the state of whether or not standard error is redirected to standard output.

param
redirectErrorStream {@code true} to redirect standard error, {@code false} otherwise.
return
this process builder instance.
since
Android 1.0

        this.redirectErrorStream = redirectErrorStream;
        return this;
    
public booleanredirectErrorStream()
Indicates whether the standard error should be redirected to standard output. If redirected, the {@link Process#getErrorStream()} will always return end of stream and standard error is written to {@link Process#getInputStream()}.

return
{@code true} if the standard error is redirected; {@code false} otherwise.
since
Android 1.0

        return redirectErrorStream;
    
public java.lang.Processstart()
Starts a new process based on the current state of this process builder.

return
the new {@code Process} instance.
throws
NullPointerException if any of the elements of {@link #command()} is {@code null}.
throws
IndexOutOfBoundsException if {@link #command()} is empty.
throws
SecurityException if {@link SecurityManager#checkExec(String)} doesn't allow process creation.
throws
IOException if an I/O error happens.
since
Android 1.0

        if (command.isEmpty()) {
            throw new IndexOutOfBoundsException();
        }
        String[] cmdArray = new String[command.size()];
        for (int i = 0; i < cmdArray.length; i++) {
            if ((cmdArray[i] = command.get(i)) == null) {
                throw new NullPointerException();
            }
        }
        String[] envArray = new String[environment.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : environment.entrySet()) {
            envArray[i++] = entry.getKey() + "=" + entry.getValue(); //$NON-NLS-1$
        }
        Process process = Runtime.getRuntime().exec(cmdArray, envArray,
                directory);
        // TODO implement support for redirectErrorStream
        return process;
    
private static java.util.ListtoList(java.lang.String[] strings)

        ArrayList<String> arrayList = new ArrayList<String>(strings.length);
        for (String string : strings) {
            arrayList.add(string);
        }
        return arrayList;