Execuate a system command
Process exec(String command)
- Executes the specified string command in a separate process.
Process exec(String[] cmdarray)
- Executes the specified command and arguments in a separate process.
Process exec(String[] cmdarray, String[] envp)
- Executes the specified command and arguments in a separate process with the specified environment.
Process exec(String[] cmdarray, String[] envp, File dir)
- Executes the specified command and arguments in a separate process with the specified environment and working directory.
Process exec(String command, String[] envp)
- Executes the specified string command in a separate process with the specified environment.
Process exec(String command, String[] envp, File dir)
- Executes the specified string command in a separate process with the specified environment and working directory.
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
try {
Process pro = runtime.exec("notepad.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Wait until notepad is terminated.
public class Main {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec("notepad");
p.waitFor();
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
System.out.println("Notepad returned " + p.exitValue());
}
}