What is the purpose of Process class in Java?

JavaObject Oriented ProgrammingProgramming

The java.lang.Process is a subclass of Object class and it can describe the processes that are started by the exec() method of Runtime class. A Process object controls the process and gets information about it. The Process class is an abstract class, therefore, it cannot be instantiated. The important method s of the Process class are destroy(), exitValue(), getErrorStream(), waitFor(), getInputStream() and getOutputStream().

Syntax

public abstract class Process extends Object

Example

import java.util.concurrent.*;
public class ProcessTest {
   public static void main(String[] args) throws Exception {
      Runtime runtime = Runtime.getRuntime();
      System.out.println("Launching of Notepad Application");
      Process process = runtime.exec("Notepad.exe"); // Launch a Notepad application
      System.out.println("Wait for 5 seconds");
      p.waitFor(5, TimeUnit.SECONDS);
      System.out.println("Exit of Notepad Application");
      process.destroy(); // destroy the application
   }
}

In the above program, we are implementing a Process class. Whenever we can call the exec("Notepad.exe") method of Runtime class, it launches the notepad application and destroys the application after 5 seconds.

Output

Launching of Notepad Application
Wait for 5 seconds
Exit of Notepad Application
raja
Published on 31-Jul-2019 16:26:01
Advertisements