Java – Run shell command in java

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();
        }
    }
}

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post