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

StringexecuteCommand(String command)
execute Command
Process proc = Runtime.getRuntime().exec(command);
try (InputStream stream = proc.getInputStream()) {
    try (Scanner s = new Scanner(stream).useDelimiter("\\A")) {
        return s.hasNext() ? s.next() : "";
StringexecuteCommand(String command)
execute Command
StringBuffer output = new StringBuffer();
Process p;
try {
    p = Runtime.getRuntime().exec(command);
    p.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
...
StringexecuteCommand(String command, boolean waitForResponse)
execute Command
String response = "";
ProcessBuilder pb = null;
pb = new ProcessBuilder("bash", "-c", command);
pb.redirectErrorStream(true);
if (DEBUG) {
    System.out.println("Command: " + command);
try {
...
StringexecuteCommand(String[] command, boolean wait)
execute Command
Process p = Runtime.getRuntime().exec(command);
if (wait) {
    p.waitFor();
    return readProcessOutput(p);
return null;
StringexecuteCommand1(boolean isProcessBuilder, String command)
execute Command
try {
    ProcessBuilder broker = new ProcessBuilder("cmd", "-c", "dir");
    Process runBroker = broker.start();
    Reader reader = new InputStreamReader(runBroker.getInputStream());
    int ch;
    while ((ch = reader.read()) != -1)
        System.out.print((char) ch);
    reader.close();
...
StringexecuteCommand2(boolean isProcessBuilder, String command)
execute Command
ProcessBuilder dirProcess = new ProcessBuilder("java", "-Xmx1024m", "-Xms1024m",
        "-DTOOLS_DIR=/home/IM/work/dist", "-Daoi=whole", "-jar", "/home/IM/work/dist/idt_tools.jar");
File commands = new File("C:/process/commands.bat");
File dirOut = new File("C:/process/out.txt");
File dirErr = new File("C:/process/err.txt");
dirProcess.start();
return null;
StringexecuteCommandAndExtractStdOut(String cmd)
execute Command And Extract Std Out
String line;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder allS = new StringBuilder();
while ((line = bri.readLine()) != null) {
    allS.append(line + "\n");
bri.close();
...
voidexecuteCommandForceDir(String baseCommand, String osPath, File file)
execute Command Force Dir
String forceDirectoryPath = osPath;
if (file.isFile())
    try {
        forceDirectoryPath = file.getParentFile().getCanonicalPath();
    } catch (IOException ex) {
        ex.printStackTrace();
String args[] = { baseCommand, forceDirectoryPath };
...
String[]executeCommandLine(String command)
execute Command Line
List<String> lines = new ArrayList<String>();
String line;
Process proc;
proc = Runtime.getRuntime().exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = input.readLine()) != null) {
    lines.add(line);
input.close();
if (0 == proc.waitFor()) {
    proc.destroy();
    return lines.toArray(new String[lines.size()]);
return lines.toArray(new String[0]);
String[]executeCommandLineReturnAll(final String[] command)
execute Command Line Return All
final ArrayList<String> al = new ArrayList<String>();
try {
    final Runtime rt = Runtime.getRuntime();
    rt.traceInstructions(true);
    rt.traceMethodCalls(true);
    final Process p = rt.exec(command);
    final BufferedReader data = new BufferedReader(new InputStreamReader(p.getInputStream()));
    final BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
...