Java Shell Command runShell(String cmd)

Here you can find the source of runShell(String cmd)

Description

run Shell

License

Apache License

Declaration

public static boolean runShell(String cmd) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static boolean runShell(String cmd) {
        String[] cmds = new String[3];
        cmds[0] = "/bin/sh";
        cmds[1] = "-c";
        cmds[2] = cmd;//www.j  ava2  s.c o  m

        System.out.println("shell command: ");
        System.out.println(cmd);

        try {
            Process process = Runtime.getRuntime().exec(cmds);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            process.waitFor();

            br.close();
            Thread.sleep(1000);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. runCommandArray(String[] command)
  2. runCommandGetOutput(String startFolder, String[] args, int[] cmdReturnValue)
  3. runCommandPrompt(final String commandLine, final IPath path)
  4. runCommandWithOutput(String cmd)
  5. runShell(File workspace, String... shellElements)
  6. runShell(String shStr)
  7. runShellCommand(String command)
  8. runShellCommand(String command)
  9. runShellCommand(String command)