Java exec execShell(String shell)

Here you can find the source of execShell(String shell)

Description

exec Shell

License

Open Source License

Declaration

public static String execShell(String shell) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Main {

    public static String execShell(String shell) throws IOException {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec(shell);
        return getOutputMsg(process);
    }//from   w  ww.  j a v  a  2s .com

    public static String getOutputMsg(Process process) throws IOException {
        StringBuilder sb = new StringBuilder();
        LineNumberReader input = null;
        InputStreamReader ir = null;
        try {

            ir = new InputStreamReader(process.getInputStream());
            input = new LineNumberReader(ir);
            String line;
            while ((line = input.readLine()) != null) {
                sb.append(line);
                System.out.println(line);
            }
        } finally {
            if (input != null)
                input.close();
            if (ir != null)
                ir.close();
        }
        return sb.toString();
    }
}

Related

  1. execProcess(String process)
  2. execProcess(String[] cmdline, final long timeout)
  3. Execption2Strings(boolean rep, Throwable... execptions)
  4. execScript(File shellScriptFile, String[] scriptCommand, PrintWriter execLog)
  5. execShell(String cmd)
  6. execSudoCommand(final String _sudoCmd, final String _pw)
  7. execSystemCommand(String[] commands, String executionPath)
  8. execToString(final String... args)
  9. execute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd, File dir, String[] env)