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 :)
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.
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.
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.
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.
Whenever in doubt, always consult the API:
java.lang.Process
The
ProcessBuilder.start()
andRuntime.exec
methods create a native process and return an instance of a subclass ofProcess
that can be used to control the process and obtain information about it. The classProcess
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.
java.lang.Process
java.lang.ProcessBuilder
java.lang.Runtime
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());
}
}
It's an object-based representation of a process. Similar to the Thread class, which represents a thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With