Java.lang.Process class in Java (original) (raw)
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
public
class
ProcessDemo {
`` public
static
void
main(String[] args)
`` {
`` try
{
`` System.out.println(
"Creating Process"
);
`` ProcessBuilder builder =
new
ProcessBuilder(
"notepad.exe"
);
`` Process pro = builder.start();
`` System.out.println(
"Waiting"
);
`` Thread.sleep(
10000
);
`` pro.destroyForcibly();
`` System.out.println(
"Process destroyed"
);
`` }
`` catch
(Exception ex) {
`` ex.printStackTrace();
`` }
`` }
}
Output:
Creating Process Waiting Process destroyed
Java
import
java.io.*;
import
java.lang.*;
class
ProcessDemo {
`` public
static
void
main(String arg[])
`` throws
IOException, Exception
`` {
`` System.out.println(
"Creating process"
);
`` ProcessBuilder p =
new
ProcessBuilder(
new
String[] {
`` "open"
,
"/ Applications / Facetime.app"
`` });
`` Process pro = p.start();
`` Thread.sleep(
10000
);
`` System.out.println(
"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
public
class
ProcessDemo {
`` public
static
void
main(String[] args)
`` {
`` try
{
`` System.out.println(
"Creating Process"
);
`` ProcessBuilder builder =
new
ProcessBuilder(
"notepad.exe"
);
`` Process pro = builder.start();
`` pro.destroy();
`` System.out.println(
"exit value:"
+ pro.exitValue());
`` }
`` 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
import
java.io.*;
import
java.lang.*;
class
ProcessDemo {
`` public
static
void
main(String arg[])
`` throws
IOException, Exception
`` {
`` Runtime r = Runtime.getRuntime();
`` 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;
`` 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
import
java.io.BufferedOutputStream;
import
java.io.OutputStream;
public
class
ProcessDemo
{
`` public
static
void
main(String[] args)
`` {
`` try
`` {
`` System.out.println(
"Creating Process"
);
`` Process p = Runtime.getRuntime().exec(
"notepad.exe"
);
`` OutputStream out = p.getOutputStream();
`` 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
import
java.io.InputStream;
public
class
ProcessDemo
{
`` public
static
void
main(String[] args)
`` {
`` try
`` {
`` System.out.println(
"Creating Process"
);
`` Process p = Runtime.getRuntime().exec(
"notepad.exe"
;);
`` InputStream error = p.getErrorStream();
`` for
(
int
i =
0
; i < error.available(); i++)
`` {
`` System.out.println(
" "
+ error.read());
`` }
`` 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
public
class
ProcessDemo
{
`` public
static
void
main(String[] args)
`` {
`` try
`` {
`` System.out.println(
"Creating Process"
);
`` Process p = Runtime.getRuntime().exec(
"notepad.exe"
);
`` p.waitFor();
`` System.out.println(
"Waiting over"
);
`` }
`` catch
(Exception ex)
`` {
`` ex.printStackTrace();
`` }
`` }
}
- Output:
Creating Process... Waiting over.