Java Utililty Methods exec

List of utility methods to do exec

Description

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

Method

StringexecCommand(String command)
execute a command and return the output
Runtime runtime = Runtime.getRuntime();
Process process = null;
BufferedReader reader = null;
StringBuffer result = new StringBuffer();
try {
    process = runtime.exec(command);
    reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = null;
...
StringexecCommand(String[] cmd)
Execute a shell command
Process proc = Runtime.getRuntime().exec(cmd);
java.io.InputStream is = proc.getInputStream();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return (s.hasNext()) ? s.next().trim() : "";
StringexecCommand(String[] cmd)
exec Command
Runtime run = Runtime.getRuntime();
Process pr = null;
try {
    pr = run.exec(cmd);
} catch (IOException e) {
    e.printStackTrace();
    throw e;
try {
    pr.waitFor();
} catch (InterruptedException e) {
    e.printStackTrace();
    throw e;
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;
StringBuffer out = new StringBuffer();
try {
    while ((line = buf.readLine()) != null) {
        out.append(line);
        out.append("\n");
} catch (IOException e) {
    e.printStackTrace();
    throw e;
return out.toString();
ProcessexecCommandLineUtility(String cmd)
exec Command Line Utility
return execCommandLineUtility(cmd, System.out, null);
booleanexecGenericCommand(String[] command, String redirectOutput)
It executes a command and redirect output in the specific filename
ProcessBuilder procBild = new ProcessBuilder(command);
procBild.redirectOutput(new File(redirectOutput));
try {
    Process p = procBild.start();
    p.waitFor();
    return (p.exitValue() == 0);
} catch (IOException e) {
    e.printStackTrace();
...
StringexecGetOutput(String[] command, String[] env)
exec Get Output
Process p;
p = Runtime.getRuntime().exec(command, env);
StringBuffer outputBuf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
    outputBuf.append(line);
    outputBuf.append("\n");
...
StringexecHostName(String execCommand)
exec Host Name
InputStream stream;
Scanner s;
try {
    Process proc = Runtime.getRuntime().exec(execCommand);
    stream = proc.getInputStream();
    if (stream != null) {
        s = new Scanner(stream);
        s.useDelimiter("\\A");
...
StringexecIt(String cmd)
exec It
Process process;
String result = "";
try {
    process = Runtime.getRuntime().exec(cmd);
    BufferedInputStream in = new BufferedInputStream(process.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String lineStr;
    while ((lineStr = br.readLine()) != null) {
...
ProcessexecJAR(String jarPath, String vmArgs, String appArgs, String workDir)
Launch specified jar file.
return exec("java " + vmArgs + " -jar " + jarPath + " " + appArgs, workDir);
StringexecLocalhostCmd(String cmd)
exec Localhost Cmd
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bufReader = new BufferedReader(isr);
String line = "";
StringBuffer sbuf = new StringBuffer("");
while ((line = bufReader.readLine()) != null) {
...