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 🙂
Whenever in doubt, always consult the API:
So yes,
Runtime.execcan 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 returnedProcessobject lets you control it, kill it, and importantly sometimes, redirect its standard input/output/error streams.Related questions
API links
java.lang.Processjava.lang.ProcessBuilderjava.lang.Runtimenotepad.exe example
As mentioned before, this is platform dependent, but this snippet works on my Windows machine; it launches
notepad.exe, and attempts to opentest.txtfrom the current working directory. The program then waits for the process to terminate, and prints its exit code.