Creating Native Processes from Java: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 17: Line 17:
=Runtime.exec()=
=Runtime.exec()=


<tt>Runtime.exec()</tt> creates a new O/S process and executes the specified command and arguments in a separate process with the specified environment variables and working directory.
<tt>Runtime.exec()</tt> creates a new O/S process and executes the specified command and arguments in a separate process with the specified environment variables (each element of the String[] has a "name=value" format) and working directory.


If the environment variable array in null, the subprocess inherits the environment settings of the current Java process. If the working directory is null, the subprocess inherits the current working directory of the current process.
If the environment variable array in null, the subprocess inherits the environment settings of the current Java process. If the working directory is null, the subprocess inherits the current working directory of the current process.

Revision as of 02:20, 23 February 2016

External

Internal

Overview

ProcessBuilder.start()

ProcessBuilder.start() is the preferred way to start a process with a modified environment.

Runtime.exec()

Runtime.exec() creates a new O/S process and executes the specified command and arguments in a separate process with the specified environment variables (each element of the String[] has a "name=value" format) and working directory.

If the environment variable array in null, the subprocess inherits the environment settings of the current Java process. If the working directory is null, the subprocess inherits the current working directory of the current process.

envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process. dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process. Returns: A new Process object for managing the subprocess Throws: SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess IOException - If an I/O error occurs NullPointerException - If cmdarray is null, or one of the elements of cmdarray is null, or one of the elements of envp is null IndexOutOfBoundsException - If cmdarray is an empty array (has length 0) Since:

Canonical form:

public Process Runtime.exec(String[] cmdarray, String[] envp, File dir) throws Exception