Creating Native Processes from Java: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 13: Line 13:




<tt>ProcessBuilder.start()</tt> is now the preferred way to start a process with a modified environment.
<tt>ProcessBuilder.start()</tt> is the preferred way to start a process with a modified environment.


=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 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.


 
The call returns a new <tt>Process</tt> object for managing the subprocess.
The working directory of the new subprocess is specified by dir. If dir is null, the subprocess inherits the current working directory of the current process.
 
If a security manager exists, its checkExec method is invoked with the first component of the array cmdarray as its argument. This may result in a SecurityException being thrown.
 
Starting an operating system process is highly system-dependent. Among the many things that can go wrong are:
 
The operating system program file was not found.
Access to the program file was denied.
The working directory does not exist.
In such cases an exception will be thrown. The exact nature of the exception is system-dependent, but it will always be a subclass of IOException.
 
Parameters:
cmdarray - array containing the command to call and its arguments.
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:
Canonical form:

Latest revision as of 02:21, 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.

The call returns a new Process object for managing the subprocess.

Canonical form:

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