Java Shell Command runShellCommand(String command)

Here you can find the source of runShellCommand(String command)

Description

Run synchronous shell command and wait till it finishes

License

Apache License

Parameter

Parameter Description
command a parameter

Declaration

public static void runShellCommand(String command) 

Method Source Code


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

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

public class Main {
    /**//from   w  ww.  j  a  v  a2s  . c  o m
     * Run synchronous shell command and wait till it finishes 
     * @param command
     */
    public static void runShellCommand(String command) {
        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);
            }

            int exitVal = pr.waitFor();

            System.out.println("Exited with error code " + exitVal);

        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}

Related

  1. runCommandPrompt(final String commandLine, final IPath path)
  2. runCommandWithOutput(String cmd)
  3. runShell(File workspace, String... shellElements)
  4. runShell(String cmd)
  5. runShell(String shStr)
  6. runShellCommand(String command)
  7. runShellCommand(String command)
  8. runShellCommand(String[] cmd, StringBuilder outputLines, StringBuilder errorLines)
  9. sendCommand(String path, String cmd)