Creating Native Processes from Java: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 12: Line 12:
<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 and working directory.


Given an array of strings cmdarray, representing the tokens of a command line, and an array of strings envp, representing "environment" variable settings, this method creates a new process in which to execute the specified command.
If the environment variable array in null, the subprocess inherits the environment settings of the current Java process.


This method checks that cmdarray is a valid operating system command. Which commands are valid is system-dependent, but at the very least the command must be a non-empty list of non-null strings.
<tt>ProcessBuilder.start()</tt> is now the preferred way to start a process with a modified environment.
 
If envp is null, the subprocess inherits the environment settings of the current process.
 
A minimal set of system dependent environment variables may be required to start a process on some operating systems. As a result, the subprocess may inherit additional environment variable settings beyond those in the specified environment.
 
ProcessBuilder.start() is now the preferred way to start a process with a modified environment.


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

Revision as of 02:10, 23 February 2016

External

Internal

Overview

Runtime.exec() 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.

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

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

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:

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