Java.lang.Process class in Java
Last Updated :
15 Feb, 2023
The abstract Process class is a process that is, an executing program. Methods provided by the Process are used to perform input, and output, waiting for the process to complete, checking the exit status of the process, and destroying the process.
- It extends class Object.
- It is used primarily as a superclass for the type of object created by exec() in the Runtime class.
- ProcessBuilder.start() and Runtime.getRuntime.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.
- ProcessBuilder.start() is the most preferred way to create a process.
ProcessBuilder.start() vs Runtime.getRuntime.exec(): ProcessBuilder allows us to redirect the standard error of the child process into its standard output. Now we don't need two separate threads one reading from stdout and one reading from stderr. Constructor
- Process(): This is the only constructor.
Methods:
- void destroyForcibly(): Kills the subprocess.
Syntax: public abstract void destroyForcibly().
Returns: NA.
Exception: NA.
Java
// Java code illustrating destroyForcibly()
// method for windows operating system
// Class
public class ProcessDemo {
// Main driver method
public static void main(String[] args)
{
try {
// create a new process
System.out.println("Creating Process");
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process pro = builder.start();
// wait 10 seconds
System.out.println("Waiting");
Thread.sleep(10000);
// kill the process
pro.destroyForcibly();
System.out.println("Process destroyed");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Output:
Creating Process
Waiting
Process destroyed
Java
// Java code illustrating destroyForcibly()
// method for Mac Operating System
import java.io.*;
import java.lang.*;
class ProcessDemo {
public static void main(String arg[])
throws IOException, Exception
{
System.out.println("Creating process");
// creating process
ProcessBuilder p = new ProcessBuilder(new String[] {
"open", "/ Applications / Facetime.app"
});
Process pro = p.start();
// waiting for 10 second
Thread.sleep(10000);
System.out.println("destroying process");
// destroying process
pro.destroyForcibly();
}
}
Output:
Creating process
destroying process
int exitValue(): This method returns the exit value for the subprocess.
Syntax: public abstract int exitValue().
Returns: This method returns the exit value of
the subprocess represented by this Process object.
By convention, the value 0 indicates normal termination.
Exception: IllegalThreadStateException ,
if the subprocess represented by this Process object has not yet terminated.
Java
// Java code illustrating exitValue() method
public class ProcessDemo {
public static void main(String[] args)
{
// Try block to check for exceptions
try {
// create a new process
System.out.println("Creating Process");
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process pro = builder.start();
// kill the process
pro.destroy();
// checking the exit value of subprocess
System.out.println("exit value:" + pro.exitValue());
}
// Catch block to handle the exceptions
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Output:
Creating Process
1
abstract InputStream getErrorStream(): This method gets the input stream of the subprocess.
Syntax: public abstract InputStream getInputStream().
Returns: input stream that reads input from the process out output stream.
Exception: NA.
Java
// Java code illustrating
// getInputStream() method
import java.io.*;
import java.lang.*;
class ProcessDemo {
public static void main(String arg[])
throws IOException, Exception
{
// creating the process
Runtime r = Runtime.getRuntime();
// shell script for loop from 1 to 3
String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done"};
Process p = r.exec(nargs);
BufferedReader is = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
// reading the output
while ((line = is.readLine()) != null)
System.out.println(line);
}
}
Output:
1
2
3
abstract OutputStream getOutputStream(): This method gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object.
Syntax: public abstract OutputStream getOutputStream()
Returns: the output stream connected to the normal input of the subprocess.
Exception: NA.
Java
// Java code illustrating
// getOutputStream() method
import java.io.BufferedOutputStream;
import java.io.OutputStream;
public class ProcessDemo
{
public static void main(String[] args)
{
try
{
// create a new process
System.out.println("Creating Process");
Process p = Runtime.getRuntime().exec("notepad.exe");
// get the output stream
OutputStream out = p.getOutputStream();
// close the output stream
System.out.println("Closing the output stream");
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Output:
Creating Process...
Closing the output stream...
abstract InputStream getErrorStream(): It returns an input stream that reads input from the process err output stream.
Syntax: public abstract InputStream getErrorStream().
Returns: the input stream connected to the error stream of the subprocess.
Exception: NA.
Java
// Java code illustrating
// getErrorStream() method
import java.io.InputStream;
public class ProcessDemo
{
public static void main(String[] args)
{
try
{
// create a new process
System.out.println("Creating Process");
Process p = Runtime.getRuntime().exec("notepad.exe";);
// get the error stream of the process and print it
InputStream error = p.getErrorStream();
for (int i = 0; i < error.available(); i++)
{
System.out.println(" " + error.read());
}
// wait for 10 seconds and then destroy the process
Thread.sleep(10000);
p.destroy();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Output:
Creating Process
int waitFor(): Returns the exit code returned by the process. This method does not return until the process on which it is called terminates.
Syntax: public int waitFor().
Returns: the exit value of the process. By convention, 0 indicates normal termination.
Exception: throws InterruptedException.
Java
// Java code illustrating
// waitFor() method
public class ProcessDemo
{
public static void main(String[] args)
{
try
{
// create a new process
System.out.println("Creating Process");
Process p = Runtime.getRuntime().exec("notepad.exe");
// cause this process to stop
// until process p is terminated
p.waitFor();
// when you manually close notepad.exe
// program will continue here
System.out.println("Waiting over");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
- Output:
Creating Process...
Waiting over.
Â
Similar Reads
Java.lang.ProcessBuilder class in Java
This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the same instance to create new subprocesses wi
8 min read
Java.lang.Void Class in Java
Java.lang.Void class is a placeholder that holds a reference to a class object if it represents a void keyword. It is an uninstantiable placeholder. Well, uninstantiable means that this class has a private constructor and no other constructor that we can access from outside. Methods of lang.void cla
1 min read
Java.lang.Runtime class in Java
In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java
6 min read
java.lang.reflect.Proxy Class in Java
A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria
4 min read
Java.lang.ThreadLocal Class in Java
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. Basically, it is another way to achieve thread safety apart from writing im
4 min read
java.lang.MethodType Class in Java
MethodType is a Class that belongs to java.lang package. This class consists of various types of methods that help in most of cases to find the method type based on the input object, or parameters of the method. All the instances of methodType are immutable. This means the objects of the methodType
4 min read
java.lang.ref.Reference Class in Java
java.lang.ref.Reference Class is an abstract base class for reference object. This class contains methods used to get information about the reference objects. This class is not a direct subclass because the operations on the reference objects are in close co-operation with the garbage collector. Cla
3 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
java.lang.reflect.Field Class in Java
The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t
5 min read
Java.lang package in Java
Java.lang package in JavaProvides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Following are the Important Classes in Java.lan
3 min read