Archive for Java

Java – Run shell command in java

Sunday, December 20th, 2009

Execute Shell command can be run by Runtime class. Shell can be executed in linux or unix platform.

This example will explain how to run shell commands in java and execute commands in java.

public class JavaRunCommandExample {

    public static void main(String[] args) {

        Runtime r = Runtime.getRuntime();
        Process p = null;
        String cmd[] = {"/bin/sh","ls","-l"};

        try {
            p = r.exec(cmd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}