Java process start example

Java process start example

Here is an example that starts a process with a modified working directory and environment, and redirects standard output and error to be appended to a log file:

 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2"); Map env = pb.environment(); env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory(new File("myDir")); File log = new File("log"); pb.redirectErrorStream(true); pb.redirectOutput(Redirect.appendTo(log)); Process p = pb.start(); assert pb.redirectInput() == Redirect.PIPE; assert pb.redirectOutput().file() == log; assert p.getInputStream().read() == -1; 

To start a process with an explicit set of environment variables, first call Map.clear() before adding environment variables.

Nested Class Summary

Constructor Summary

Method Summary

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

Methods inherited from class java.lang.Object

Constructor Detail

ProcessBuilder

Constructs a process builder with the specified operating system program and arguments. This constructor does not make a copy of the command list. Subsequent updates to the list will be reflected in the state of the process builder. It is not checked whether command corresponds to a valid operating system command.

ProcessBuilder

Constructs a process builder with the specified operating system program and arguments. This is a convenience constructor that sets the process builder’s command to a string list containing the same strings as the command array, in the same order. It is not checked whether command corresponds to a valid operating system command.

Читайте также:  Html код одной кавычки

Method Detail

command

public ProcessBuilder command(ListString> command)

Sets this process builder’s operating system program and arguments. This method does not make a copy of the command list. Subsequent updates to the list will be reflected in the state of the process builder. It is not checked whether command corresponds to a valid operating system command.

command

Sets this process builder’s operating system program and arguments. This is a convenience method that sets the command to a string list containing the same strings as the command array, in the same order. It is not checked whether command corresponds to a valid operating system command.

command

Returns this process builder’s operating system program and arguments. The returned list is not a copy. Subsequent updates to the list will be reflected in the state of this process builder.

environment

Returns a string map view of this process builder’s environment. Whenever a process builder is created, the environment is initialized to a copy of the current process environment (see System.getenv() ). Subprocesses subsequently started by this object’s start() method will use this map as their environment. The returned object may be modified using ordinary Map operations. These modifications will be visible to subprocesses started via the start() method. Two ProcessBuilder instances always contain independent process environments, so changes to the returned map will never be reflected in any other ProcessBuilder instance or the values returned by System.getenv . If the system does not support environment variables, an empty map is returned. The returned map does not permit null keys or values. Attempting to insert or query the presence of a null key or value will throw a NullPointerException . Attempting to query the presence of a key or value which is not of type String will throw a ClassCastException . The behavior of the returned map is system-dependent. A system may not allow modifications to environment variables or may forbid certain variable names or values. For this reason, attempts to modify the map may fail with UnsupportedOperationException or IllegalArgumentException if the modification is not permitted by the operating system. Since the external format of environment variable names and values is system-dependent, there may not be a one-to-one mapping between them and Java’s Unicode strings. Nevertheless, the map is implemented in such a way that environment variables which are not modified by Java code will have an unmodified native representation in the subprocess. The returned map and its collection views may not obey the general contract of the Object.equals(java.lang.Object) and Object.hashCode() methods. The returned map is typically case-sensitive on all platforms. If a security manager exists, its checkPermission method is called with a RuntimePermission («getenv.*») permission. This may result in a SecurityException being thrown. When passing information to a Java subprocess, system properties are generally preferred over environment variables.

directory

Returns this process builder’s working directory. Subprocesses subsequently started by this object’s start() method will use this as their working directory. The returned value may be null — this means to use the working directory of the current Java process, usually the directory named by the system property user.dir , as the working directory of the child process.

directory

Sets this process builder’s working directory. Subprocesses subsequently started by this object’s start() method will use this as their working directory. The argument may be null — this means to use the working directory of the current Java process, usually the directory named by the system property user.dir , as the working directory of the child process.

redirectInput

public ProcessBuilder redirectInput(ProcessBuilder.Redirect source)

Sets this process builder’s standard input source. Subprocesses subsequently started by this object’s start() method obtain their standard input from this source. If the source is Redirect.PIPE (the initial value), then the standard input of a subprocess can be written to using the output stream returned by Process.getOutputStream() . If the source is set to any other value, then Process.getOutputStream() will return a null output stream.

redirectOutput

public ProcessBuilder redirectOutput(ProcessBuilder.Redirect destination)

Sets this process builder’s standard output destination. Subprocesses subsequently started by this object’s start() method send their standard output to this destination. If the destination is Redirect.PIPE (the initial value), then the standard output of a subprocess can be read using the input stream returned by Process.getInputStream() . If the destination is set to any other value, then Process.getInputStream() will return a null input stream.

redirectError

public ProcessBuilder redirectError(ProcessBuilder.Redirect destination)

Sets this process builder’s standard error destination. Subprocesses subsequently started by this object’s start() method send their standard error to this destination. If the destination is Redirect.PIPE (the initial value), then the error output of a subprocess can be read using the input stream returned by Process.getErrorStream() . If the destination is set to any other value, then Process.getErrorStream() will return a null input stream. If the redirectErrorStream attribute has been set true , then the redirection set by this method has no effect.

redirectInput

Sets this process builder’s standard input source to a file. This is a convenience method. An invocation of the form redirectInput(file) behaves in exactly the same way as the invocation redirectInput (Redirect.from(file)) .

redirectOutput

Sets this process builder’s standard output destination to a file. This is a convenience method. An invocation of the form redirectOutput(file) behaves in exactly the same way as the invocation redirectOutput (Redirect.to(file)) .

redirectError

Sets this process builder’s standard error destination to a file. This is a convenience method. An invocation of the form redirectError(file) behaves in exactly the same way as the invocation redirectError (Redirect.to(file)) .

redirectInput

Returns this process builder’s standard input source. Subprocesses subsequently started by this object’s start() method obtain their standard input from this source. The initial value is Redirect.PIPE .

redirectOutput

Returns this process builder’s standard output destination. Subprocesses subsequently started by this object’s start() method redirect their standard output to this destination. The initial value is Redirect.PIPE .

redirectError

Returns this process builder’s standard error destination. Subprocesses subsequently started by this object’s start() method redirect their standard error to this destination. The initial value is Redirect.PIPE .

inheritIO

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process. This is a convenience method. An invocation of the form

 pb.redirectInput(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT) .redirectError(Redirect.INHERIT) 

This gives behavior equivalent to most operating system command interpreters, or the standard C library function system() .

redirectErrorStream

public boolean redirectErrorStream()

Tells whether this process builder merges standard error and standard output. If this property is true , then any error output generated by subprocesses subsequently started by this object’s start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false .

redirectErrorStream

public ProcessBuilder redirectErrorStream(boolean redirectErrorStream)

Sets this process builder’s redirectErrorStream property. If this property is true , then any error output generated by subprocesses subsequently started by this object’s start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false .

start

  • The operating system program file was not found.
  • Access to the program file was denied.
  • The working directory does not exist.
  • its checkExec method doesn’t allow creation of the subprocess, or
  • the standard input to the subprocess was redirected from a file and the security manager’s checkRead method denies read access to the file, or
  • the standard output or standard error of the subprocess was redirected to a file and the security manager’s checkWrite method denies write access to the file

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью