Java Utililty Methods Shell Command

List of utility methods to do Shell Command

Description

The list of methods to do Shell Command are organized into topic(s).

Method

StringrunShell(File workspace, String... shellElements)
run Shell
ProcessBuilder pb;
Process process;
StringBuilder result = new StringBuilder();
InputStream in = null;
InputStreamReader inReader = null;
BufferedReader bufferReader = null;
StringBuilder errorMsg = new StringBuilder();
InputStream error = null;
...
booleanrunShell(String cmd)
run Shell
String[] cmds = new String[3];
cmds[0] = "/bin/sh";
cmds[1] = "-c";
cmds[2] = cmd;
System.out.println("shell command: ");
System.out.println(cmd);
try {
    Process process = Runtime.getRuntime().exec(cmds);
...
ListrunShell(String shStr)
run Shell
List<String> strList = new ArrayList();
Process process;
process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", shStr }, null, null);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
...
voidrunShellCommand(String command)
run Shell Command
new ProcessBuilder("bash", "-c", command).start();
voidrunShellCommand(String command)
run Shell Command
System.out.println("execute: " + command);
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
voidrunShellCommand(String command)
Run synchronous shell command and wait till it finishes
try {
    System.out.println(command);
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command);
    BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line = null;
    while ((line = input.readLine()) != null) {
        System.out.println(line);
...
voidrunShellCommand(String[] cmd, StringBuilder outputLines, StringBuilder errorLines)
Runs the shell command which is specified, along with its arguments, in the given String array.
Process p = Runtime.getRuntime().exec(cmd);
if (outputLines != null) {
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        outputLines.append(line);
if (errorLines != null) {
    BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String line;
    while ((line = err.readLine()) != null) {
        errorLines.append(line);
voidsendCommand(String path, String cmd)
send Command
new Thread(() -> {
    Runtime run = Runtime.getRuntime();
    try {
        Process p = run.exec(cmd, null, new File(path));
        BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
        BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
        String lineStr;
        while ((lineStr = inBr.readLine()) != null) {
...