What is the purpose of Process class in Java?
Asked Answered
E

2

10
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 :)

Eggleston answered 9/5, 2010 at 17:2 Comment(0)
G
20

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

API links


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());
    }
}
Geisha answered 9/5, 2010 at 17:5 Comment(2)
Great Thanks :). This forum is great, probably the best in the world where great programmers from all over the world in different programming languages gather. On the contrary, even the official forum of sun(oracle) is crap. The best they can answer is read books and surf google, it is their standard answer that fits all questions :) probably because even they don't know.Eggleston
@Nitesh: in their defense, reading books and surfing Google ARE valid ways to solving problems on your own. Those are important skills worth practicing, perfecting, preaching.Geisha
M
1

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

Mays answered 9/5, 2010 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.