Java exec exec(String cmd)

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

Description

exec

License

Apache License

Declaration

public static String exec(String cmd) 

Method Source Code

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

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

public class Main {
    public static String exec(String cmd) {
        try {//from www. j ava2s  . c  o  m
            String[] cmdA = { "/bin/sh", "-c", cmd };
            Process process = Runtime.getRuntime().exec(cmdA);
            LineNumberReader br = new LineNumberReader(
                    new InputStreamReader(process.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                sb.append(line).append("\n");
            }

            return sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1)
                    .toString() : sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. exec(File dir, String cmd)
  2. exec(final String cmd, final String[] args, final byte[] in, final File dir)
  3. exec(List cmd, File binDir, File workDir, boolean parseOutput, boolean tossErrorOutput)
  4. exec(String args)
  5. exec(String cmd)
  6. exec(String cmd)
  7. exec(String cmd)
  8. exec(String cmd)
  9. exec(String cmd)