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

booleanexecSystemCommand(String[] commands, String executionPath)
exec System Command
try {
    File handler = new File(executionPath);
    Runtime rt = Runtime.getRuntime();
    Process p1 = rt.exec(commands, null, handler);
    p1.waitFor();
} catch (InterruptedException e) {
    e.printStackTrace();
    return false;
...
StringexecToString(final String... args)
Execute a system command and return the output.
ProcessBuilder pb = new ProcessBuilder(args);
Process p = pb.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
    final StringBuilder result = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        result.append(line);
        result.append('\n');
...
Stringexecute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd, File dir, String[] env)
execute
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
try {
    Process proc = Runtime.getRuntime().exec(cmd, env, dir);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    String line = null;
    while ((line = stdInput.readLine()) != null) {
...
voidexecute(final String applicationName, final String workingDirectory)
Run an external application from a given directory.
Runtime.getRuntime().exec(
        new String[] { new File(workingDirectory + File.separator + applicationName).getAbsolutePath() },
        null, new File(workingDirectory).getAbsoluteFile());
Stringexecute(final String command)
execute
StringBuilder output = new StringBuilder();
Process p;
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
    output.append(line).append("\n");
...
Stringexecute(ProcessBuilder builder, String errorMsg)
execute
try {
    final Process proc = builder.redirectErrorStream(true).start();
    final int result = proc.waitFor();
    final StringBuilder res = new StringBuilder();
    try (final BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
        while (in.ready()) {
            res.append(in.readLine()).append("\n");
    if (result != 0) {
        throw new RuntimeException(errorMsg + result + ":\n" + builder.command() + "\n" + res.toString());
    return res.toString();
} catch (IOException | InterruptedException e) {
    throw new RuntimeException(e);
String[]execute(String cmd, String input)
execute
System.out.println("--> " + cmd);
ArrayList output = new ArrayList();
Runtime runtime = Runtime.getRuntime();
try {
    Process process = runtime.exec(cmd);
    if (input != null) {
        OutputStream os = process.getOutputStream();
        OutputStreamWriter osr = new OutputStreamWriter(os);
...
booleanexecute(String command)
execute
try {
    Process p = Runtime.getRuntime().exec(command);
    return (p.waitFor() == 0);
} catch (IOException e) {
    return false;
} catch (InterruptedException e) {
    return false;
String[]execute(String command)
execute
return execute(command, null);
String[]execute(String command)

Executes the specified string command in a separate process.

ArrayList<String> list = new ArrayList<String>();
Process p = Runtime.getRuntime().exec(command);
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
    line = line.trim();
...