Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Process class in Java?

Runtime objRuntime = Runtime.getRuntime();
String strBackupString = "mysqldump -u " + userName + " -p" + password + " " + dbName;
Process objProcess = objRuntime.exec(strBackupString);

This is used for backup of database. But what exactly happens? Can anybody make me explain, what is the purpose of Runtime and Process class?

Is this class used to act as if we are typing command from command prompt? Then what should i pass to objRuntime.exec() if i want to open notepad? And is the command executed as soon as we call exec method? If yes, then what purpose does Process serve here? I really can't understand these two classes. Please make me understand. Thanks in advance :)

like image 694
TCM Avatar asked May 09 '10 17:05

TCM


People also ask

What is a process class?

Process classes are a collection of operations that encapsulate a business process. Process classes do not have data maintenance operations, or any relationship with database tables. Process classes manipulate other entity and process classes to implement a business process.

What is the purpose of a class in Java?

A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

What is the purpose of a method Java?

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

What is process API in Java?

In Java 9 Process API which is responsible to control and manage operating system processes has been improved considerably. ProcessHandle Class now provides process's native process ID, start time, accumulated CPU time, arguments, command, user, parent process, and descendants.


2 Answers

Whenever in doubt, always consult the API:

java.lang.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 class Process 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.

Runtime.exec(String command)

Executes the specified system command in a separate process.

So yes, Runtime.exec can execute a command that you'd usually type in the system command prompt. This is hardly a platform-independent solution, but sometimes it's needed. The returned Process object lets you control it, kill it, and importantly sometimes, redirect its standard input/output/error streams.

Related questions

  • Is java Runtime.exec platform independent?
  • How to create a process in Java
  • Get output from a process
  • Runtime.getRuntime().exec()

API links

  • java.lang.Process
  • java.lang.ProcessBuilder
  • java.lang.Runtime

notepad.exe example

As mentioned before, this is platform dependent, but this snippet works on my Windows machine; it launches notepad.exe, and attempts to open test.txt from the current working directory. The program then waits for the process to terminate, and prints its exit code.

public class ExecExample {
    public static void main(String[] args) throws Exception {
        Process p = Runtime.getRuntime().exec("notepad.exe test.txt");      
        System.out.println("Waiting for notepad to exit...");
        System.out.println("Exited with code " + p.waitFor());
    }
}
like image 136
polygenelubricants Avatar answered Sep 30 '22 04:09

polygenelubricants


It's an object-based representation of a process. Similar to the Thread class, which represents a thread.

like image 45
someguy Avatar answered Sep 30 '22 02:09

someguy