The various overloads of Runtime.getRuntime().exec(...) take either an array of strings or a single string. The single-string overloads of exec() will tokenise the string into an array of arguments, before passing the string array onto one of the exec() overloads that takes a string array. The ProcessBuilder constructors, on the other hand, only take a varargs array of strings or a List of strings, where each string in the array or list is assumed to be an individual argument. Either way, the arguments obtained are then joined up into a string that is passed to the OS to execute.

So, for example, on Windows,

CopyRuntime.getRuntime().exec("C:\DoStuff.exe -arg1 -arg2");

will run a DoStuff.exe program with the two given arguments. In this case, the command-line gets tokenised and put back together. However,

CopyProcessBuilder b = new ProcessBuilder("C:\DoStuff.exe -arg1 -arg2");

will fail, unless there happens to be a program whose name is DoStuff.exe -arg1 -arg2 in C:\. This is because there's no tokenisation: the command to run is assumed to have already been tokenised. Instead, you should use

CopyProcessBuilder b = new ProcessBuilder("C:\DoStuff.exe", "-arg1", "-arg2");

or alternatively

CopyList<String> params = java.util.Arrays.asList("C:\DoStuff.exe", "-arg1", "-arg2");
ProcessBuilder b = new ProcessBuilder(params);
Answer from Luke Woodward on Stack Overflow
Top answer
1 of 4
113

The various overloads of Runtime.getRuntime().exec(...) take either an array of strings or a single string. The single-string overloads of exec() will tokenise the string into an array of arguments, before passing the string array onto one of the exec() overloads that takes a string array. The ProcessBuilder constructors, on the other hand, only take a varargs array of strings or a List of strings, where each string in the array or list is assumed to be an individual argument. Either way, the arguments obtained are then joined up into a string that is passed to the OS to execute.

So, for example, on Windows,

CopyRuntime.getRuntime().exec("C:\DoStuff.exe -arg1 -arg2");

will run a DoStuff.exe program with the two given arguments. In this case, the command-line gets tokenised and put back together. However,

CopyProcessBuilder b = new ProcessBuilder("C:\DoStuff.exe -arg1 -arg2");

will fail, unless there happens to be a program whose name is DoStuff.exe -arg1 -arg2 in C:\. This is because there's no tokenisation: the command to run is assumed to have already been tokenised. Instead, you should use

CopyProcessBuilder b = new ProcessBuilder("C:\DoStuff.exe", "-arg1", "-arg2");

or alternatively

CopyList<String> params = java.util.Arrays.asList("C:\DoStuff.exe", "-arg1", "-arg2");
ProcessBuilder b = new ProcessBuilder(params);
2 of 4
26

There are no difference between ProcessBuilder.start() and Runtime.exec() because implementation of Runtime.exec() is:

Copypublic Process exec(String command) throws IOException {
    return exec(command, null, null);
}

public Process exec(String command, String[] envp, File dir)
    throws IOException {
    if (command.length() == 0)
        throw new IllegalArgumentException("Empty command");

    StringTokenizer st = new StringTokenizer(command);
    String[] cmdarray = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        cmdarray[i] = st.nextToken();
    return exec(cmdarray, envp, dir);
}

public Process exec(String[] cmdarray, String[] envp, File dir)
    throws IOException {
    return new ProcessBuilder(cmdarray)
        .environment(envp)
        .directory(dir)
        .start();
}

So code:

CopyList<String> list = new ArrayList<>();
new StringTokenizer(command)
.asIterator()
.forEachRemaining(str -> list.add((String) str));
new ProcessBuilder(String[])list.toArray())
            .environment(envp)
            .directory(dir)
            .start();

should be the same as:

CopyRuntime.exec(command)

Thanks dave_thompson_085 for comment

🌐
Java-tips
java-tips.org › home › java se tips › java.util › from runtime.exec() to processbuilder
From Runtime.exec() to ProcessBuilder - Java Tips
With ProcessBuilder, you call start() to execute the command. Prior to calling start(), you can manipulate how the Process will be created. If you want the process to start in a different directory, you don't pass a File in as a command line argument.
Discussions

java - ProcessBuilder vs Runtime.exec() - Stack Overflow
Which one is better? By better I mean which one has better security, etc. (not ease of use). More on stackoverflow.com
🌐 stackoverflow.com
November 25, 2011
Disadvantages of using Runtime.exec()?
Exception handling for one. By using exec you don't get any results, unlike the Java functions. More on reddit.com
🌐 r/java
13
8
April 13, 2020
java - Order a Runtime exec and a ProcessBuilder.start? - Stack Overflow
I've been confronted to a weird problem while running and killing processes through java. Basically, I have a method which kills one process using taskkill : private static void kill() { try... More on stackoverflow.com
🌐 stackoverflow.com
Frequent 'processbuilder' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
🌐
OpenJDK
openjdk.org › jeps › 8263697
JEP draft: Safer Process Launch by ProcessBuilder and Runtime.exec
The executable is recognized by Windows and ProcessBuilder as an .exe if the file name ends in case-insensitive .EXE or does not have a dot in the filename. The special characters for .exe and non-.exe that are to be quoted are defined by Windows for [C++ command line arguments] exe-quotes and Cmd arguments as: ... ProcessBuilder and Runtime.exec handle argument encoding and passing without the application needing to add or modify the argument except in a few unusual cases.
🌐
Rip Tutorial
riptutorial.com › pitfall: runtime.exec, process and processbuilder don't understand shell syntax
Java Language Tutorial => Pitfall: Runtime.exec, Process and...
(The first example lists the names of all Java files in the file system, and the second one prints the package statements2 in the Java files in the "source" tree.) These are not going to work as expected. In the first case, the "find" command will be run with "2>/dev/null" as a command argument. It will not be interpreted as a redirection. In the second example, the pipe character ("|") and the works following it will be given to the "find" command. The problem here is that the exec methods and ProcessBuilder do not understand any shell syntax.
🌐
Reddit
reddit.com › r/java › disadvantages of using runtime.exec()?
r/java on Reddit: Disadvantages of using Runtime.exec()?
April 13, 2020 -

I'm working with some legacy code and have to remake a software module. The module previously used Runtime.exec() for things such as making a directory, removing a directory, unzipping a file, and copying files. The software runs on Linux servers and most probably will always run on a Linux server.

Now that I'm making the module from scratch, I'm thinking of using native Java functions for accomplishing all this. Keeping the project requirements/dependencies aside for a moment, is there any advantage/disadvantage of doing it this way?

🌐
Medium
medium.com › codex › on-command-injection-over-javas-processbuilder-8d9f833c808c
On Command Injection over Java’s ProcessBuilder | by CodeThreat | CodeX | Medium
July 5, 2021 - It’s wiser not using any shell support, cmd or sh or any other interpreter, when feeding the ProcessBuilder or Runtime.exec.
Find elsewhere
🌐
JavaPrograming
javaprograming.wordpress.com › 2009 › 04 › 23 › whats-the-difference-between-runtimeexec-and-processbuilder
What’s the difference between Runtime.exec() and ProcessBuilder? | JavaPrograming
April 23, 2009 - JDK 5.0 added a new class, ProcessBuilder to do the equivalent of the exec() method of Runtime. In addition to running commands in forked off subprocesses, you can adjust environment variables and change the starting working directory with ...
🌐
Stack Overflow
stackoverflow.com › questions › tagged › processbuilder
Frequent 'processbuilder' Questions - Stack Overflow
I'm building a process in Java using ProcessBuilder as follows: ProcessBuilder pb = new ProcessBuilder() .command("somecommand", "arg1", "arg2") .redirectErrorStream(true); Process p =... ... I'm trying to execute an external command from java code, but there's a difference I've noticed between Runtime.getRuntime().exec(...) and new ProcessBuilder(...).start().
🌐
Coderanch
coderanch.com › t › 378652 › java › processbuilder-runtime-exec
processbuilder and runtime.exec (Java in General forum at Coderanch)
December 18, 2005 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... hi all, please give me difference between ProcessBuilder class and Runtime.exec method please dont give me link if possible please give me some code and one more problem //Process p = new ProcessBuilder("myCommand", "myArg").start(); i am created processbuilder object and pass wmplayer ProcessBuilder p
🌐
Stack Overflow
stackoverflow.com › questions › 32302140 › java-runtime-or-processbuilder-or-other
Java Runtime OR Processbuilder OR Other - Stack Overflow
August 30, 2015 - My current Runtime.exec() class works fine in Eclipse running as a Java application, but once I build it and run from an actually command prompt, it fails with the above exception. So now I'm reading that we should be using ProcessBuilder to do command line executables to the OS platform.
🌐
Real's HowTo
rgagnon.com › javadetails › java-0014.html
Execute an external program - Real's Java How-to
// win xp import java.io.*; public ... (set the errorlevel 0 (execution Ok) ... Since 1.5, the ProcessBuilder class provides more controls overs the process to be started....
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Process.html
Process (Java SE 21 & JDK 21)
January 20, 2026 - Process provides control of native processes started by ProcessBuilder.start and Runtime.exec.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exec-method
Java Runtime exec() Method with Examples - GeeksforGeeks
October 23, 2023 - The ProcessBuilder class provides more control over the process configuration and is recommended for complex interactions. The exec() method has six variants, each tailored to different scenarios: exec(String command) : This variant takes a ...
🌐
Baeldung
baeldung.com › home › java › core java › how to run a shell command in java
How to Run a Shell Command in Java | Baeldung
January 8, 2024 - In this article, we’ll learn how to execute a shell command from Java applications. First, we’ll use the .exec() method the Runtime class provides. Then, we’ll learn about ProcessBuilder, which is more customizable.