run static method
Starts a process and runs it non-interactively to completion. The
process run is executable
with the specified arguments
.
Using an absolute path for executable
is recommended since resolving
the executable
path is platform-specific. On Windows, both any PATH
set in the environment
map parameter and the path set in
workingDirectory
parameter are ignored for the purposes of resolving
the executable
path.
Use workingDirectory
to set the working directory for the process. Note
that the change of directory occurs before executing the process on some
platforms, which may have impact when using relative paths for the
executable and the arguments.
Use environment
to set the environment variables for the process. If not
set the environment of the parent process is inherited. Currently, only
US-ASCII environment variables are supported and errors are likely to occur
if an environment variable with code-points outside the US-ASCII range is
passed in.
If includeParentEnvironment
is true
, the process's environment will
include the parent process's environment, with environment
taking
precedence. Default is true
.
If runInShell
is true, the process will be spawned through a system
shell. On Linux and OS X, /bin/sh
is used, while
%WINDIR%\system32\cmd.exe
is used on Windows.
NOTE: On Windows, if executable
is a batch file
('.bat' or '.cmd'), it may be launched by the operating system in a
system shell regardless of the value of runInShell
. This could result in
arguments being parsed according to shell rules. For example:
void main() async {
// Will launch notepad.
await Process.run('test.bat', ['test¬epad.exe']);
}
The encoding used for decoding stdout
and stderr
into text is
controlled through stdoutEncoding
and stderrEncoding
. The
default encoding is systemEncoding. If null
is used no
decoding will happen and the ProcessResult will hold binary
data.
Returns a Future<ProcessResult>
that completes with the
result of running the process, i.e., exit code, standard out and
standard in.
The following code uses Process.run
to grep for main
in the
file test.dart
on Linux.
var result = await Process.run('grep', ['-i', 'main', 'test.dart']);
stdout.write(result.stdout);
stderr.write(result.stderr);
Implementation
external static Future<ProcessResult> run(
String executable,
List<String> arguments, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
Encoding? stdoutEncoding = systemEncoding,
Encoding? stderrEncoding = systemEncoding,
});