Posts Tagged runtime

Java – How to execute command in java

Wednesday, December 16th, 2009

Runtime class is part of java.lang package. Runtime class helps us to execute command in operating system with process class. exec() method execute command in java with command in array string. This example will execute to open notepad in window.

Java runtime class example explains How to execute a command java.

public class JavaRunTimeCommandExecuteExample {

    public static void main(String[] args) {

        Runtime r = Runtime.getRuntime();
        Process p = null;
        String cmd[] = {"notepad.exe"};
        try {
            p = r.exec(cmd);
        } catch (Exception e) {
            System.out.println("error executing " + cmd[0]);
        }
    }
}
1