Class process start java

Class process start java

Process provides control of native processes started by ProcessBuilder.start and Runtime.exec. The class provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. By default, the created process does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream() , getInputStream() , and getErrorStream() . The parent process uses these streams to feed input to and get output from the process. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the process may cause the process to block, or even deadlock. Where desired, process I/O can also be redirected using methods of the ProcessBuilder class. The process is not killed when there are no more references to the Process object, but rather the process continues executing asynchronously. There is no requirement that the process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object. As of 1.5, ProcessBuilder.start() is the preferred way to create a Process . Subclasses of Process should override the onExit() and toHandle() methods to provide a fully functional Process including the process id, information about the process, direct children, and direct children plus descendants of those children of the process. Delegating to the underlying Process or ProcessHandle is typically easiest and most efficient.

Читайте также:  Готовый header html css

Constructor Summary

Method Summary

Returns true if the implementation of destroy() is to normally terminate the process, Returns false if the implementation of destroy forcibly and immediately terminates the process.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated, or the specified waiting time elapses.

Methods declared in class java.lang.Object

Constructor Detail

Process

Method Detail

getOutputStream

Returns the output stream connected to the normal input of the process. Output to the stream is piped into the standard input of the process represented by this Process object. If the standard input of the process has been redirected using ProcessBuilder.redirectInput then this method will return a null output stream. Implementation note: It is a good idea for the returned output stream to be buffered.

getInputStream

Returns the input stream connected to the normal output of the process. The stream obtains data piped from the standard output of the process represented by this Process object. If the standard output of the process has been redirected using ProcessBuilder.redirectOutput then this method will return a null input stream. Otherwise, if the standard error of the process has been redirected using ProcessBuilder.redirectErrorStream then the input stream returned by this method will receive the merged standard output and the standard error of the process. Implementation note: It is a good idea for the returned input stream to be buffered.

getErrorStream

Returns the input stream connected to the error output of the process. The stream obtains data piped from the error output of the process represented by this Process object. If the standard error of the process has been redirected using ProcessBuilder.redirectError or ProcessBuilder.redirectErrorStream then this method will return a null input stream. Implementation note: It is a good idea for the returned input stream to be buffered.

Читайте также:  Parent content in javascript

waitFor

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the process has already terminated. If the process has not yet terminated, the calling thread will be blocked until the process exits.

waitFor

public boolean waitFor​(long timeout, TimeUnit unit) throws InterruptedException

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated, or the specified waiting time elapses. If the process has already terminated then this method returns immediately with the value true . If the process has not terminated and the timeout value is less than, or equal to, zero, then this method returns immediately with the value false . The default implementation of this methods polls the exitValue to check if the process has terminated. Concrete implementations of this class are strongly encouraged to override this method with a more efficient implementation.

exitValue

public abstract int exitValue()

destroy

public abstract void destroy()

Kills the process. Whether the process represented by this Process object is normally terminated or not is implementation dependent. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The CompletableFuture from onExit() is completed when the process has terminated.

destroyForcibly

Kills the process forcibly. The process represented by this Process object is forcibly terminated. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The CompletableFuture from onExit() is completed when the process has terminated. Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) forcibly terminate the process.

supportsNormalTermination

public boolean supportsNormalTermination()

Returns true if the implementation of destroy() is to normally terminate the process, Returns false if the implementation of destroy forcibly and immediately terminates the process. Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) return true or false depending on the platform implementation.

isAlive

pid

Returns the native process ID of the process. The native process ID is an identification number that the operating system assigns to the process.

onExit

Returns a CompletableFuture for the termination of the Process. The CompletableFuture provides the ability to trigger dependent functions or actions that may be run synchronously or asynchronously upon process termination. When the process has terminated the CompletableFuture is completed regardless of the exit status of the process. Calling onExit().get() waits for the process to terminate and returns the Process. The future can be used to check if the process is done or to wait for it to terminate. Cancelling the CompletableFuture does not affect the Process. Processes returned from ProcessBuilder.start() override the default implementation to provide an efficient mechanism to wait for process exit.

API Note: Using onExit is an alternative to waitFor that enables both additional concurrency and convenient access to the result of the Process. Lambda expressions can be used to evaluate the result of the Process execution. If there is other processing to be done before the value is used then onExit is a convenient mechanism to free the current thread and block only if and when the value is needed.
For example, launching a process to compare two files and get a boolean if they are identical:

 Process p = new ProcessBuilder("cmp", "f1", "f2").start(); Future identical = p.onExit().thenApply(p1 -> p1.exitValue() == 0); . if (identical.get())

The process may be observed to have terminated with isAlive() before the ComputableFuture is completed and dependent actions are invoked. Implementation Requirements: This implementation executes waitFor() in a separate thread repeatedly until it returns successfully. If the execution of waitFor is interrupted, the thread’s interrupt status is preserved. When waitFor() returns successfully the CompletableFuture is completed regardless of the exit status of the process. This implementation may consume a lot of memory for thread stacks if a large number of processes are waited for concurrently. External implementations should override this method and provide a more efficient implementation. For example, to delegate to the underlying process, it can do the following:

 public CompletableFuture onExit() < return delegate.onExit().thenApply(p ->this); > 

toHandle

Returns a ProcessHandle for the Process. Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) implement toHandle as the equivalent of ProcessHandle.of(pid) including the check for a SecurityManager and RuntimePermission(«manageProcess») .

info

Returns a snapshot of information about the process. A ProcessHandle.Info instance has accessor methods that return information about the process if it is available.

children

Returns a snapshot of the direct children of the process. The parent of a direct child process is the process. Typically, a process that is not alive has no children. Note that processes are created and terminate asynchronously. There is no guarantee that a process is alive.

descendants

public StreamProcessHandle> descendants()

Returns a snapshot of the descendants of the process. The descendants of a process are the children of the process plus the descendants of those children, recursively. Typically, a process that is not alive has no children. Note that processes are created and terminate asynchronously. There is no guarantee that a process is alive.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

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