🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.processbuilder api
Guide to java.lang.ProcessBuilder API | Baeldung
November 26, 2025 - In the next section, we’re going to take a look at what additions were made to the ProcessBuilder API in Java 9. When working with the ProcessBuilder API, we may need to pass arguments that contain spaces, such as strings with multiple words or file paths under directories. If not handled correctly, these arguments can cause the command to fail or be misinterpreted by the operating system. ProcessBuilder expects each argument to be passed as a separate string in the constructor. For example, to execute:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 8 )
3 weeks ago - Modifying a process builder's attributes will affect processes subsequently started by that object's start() method, but will never affect previously started processes or the Java process itself. Most error checking is performed by the start() method. It is possible to modify the state of an object so that start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless start() is invoked. Note that this class is not synchronized. If multiple threads access a ProcessBuilder instance concurrently, and at least one of the threads modifies one of the attributes structurally, it must be synchronized externally.
🌐
Mkyong
mkyong.com › home › java › java processbuilder examples
Java ProcessBuilder examples - Mkyong.com
January 19, 2019 - ProcessBuilder processBuilder = new ProcessBuilder(); // -- Linux -- // Run a shell command processBuilder.command("bash", "-c", "ls /home/mkyong/"); // Run a shell script processBuilder.command("path/to/hello.sh"); // -- Windows -- // Run a ...
🌐
Alvin Alexander
alvinalexander.com › java › java-exec-processbuilder-process-1
Java exec: Execute system processes with Java ProcessBuilder and Process (part 1) | alvinalexander.com
November 30, 2019 - Using this class you can even execute command pipelines from Java (such as "ps aux | wc -l"), as well as sudo commands. But, first things first ... ... alvinalexander.com is owned and operated by Valley Programming, LLC In regards to links to Amazon.com, As an Amazon Associate I (Valley ...
🌐
ZetCode
zetcode.com › java › processbuilder
Java ProcessBuilder - creating processes in Java
May 1, 2025 - In order to run a command on Windows machine, we could use the following: processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com"). ... The process is lauched with start. try (var reader = new BufferedReader( new InputStreamReader(process.getInputStream()))) { With the getInputStream ...
🌐
Stack Overflow
stackoverflow.com › questions › 74168927 › processbuilder-execute-command-on-cmd
java - ProcessBuilder execute command on cmd - Stack Overflow
ProcessBuilder pb = new ProcessBuilder("cmd.exe"); pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); pb.redirectError(ProcessBuilder.Redirect.INHERIT); Process p = pb.start(); p.waitFor(); System.out.println("process exited with " + ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 7 )
Modifying a process builder's attributes will affect processes subsequently started by that object's start() method, but will never affect previously started processes or the Java process itself. Most error checking is performed by the start() method. It is possible to modify the state of an object so that start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless start() is invoked. Note that this class is not synchronized. If multiple threads access a ProcessBuilder instance concurrently, and at least one of the threads modifies one of the attributes structurally, it must be synchronized externally.
🌐
Medium
mustafa-aydogan.medium.com › how-to-run-a-command-from-java-fe6b942b98a
How to Run a Command from Java. Sometimes, you may want to run a… | by Mustafa Aydoğan | Medium
September 27, 2025 - Here are the steps to run a command from a Java process using ProcessBuilder: First, you need to create a ProcessBuilder object with the command you want to run and any arguments that are required.
Top answer
1 of 4
9

The java.lang.ProcessBuilder and java.lang.Process classes are available for executing and communicating with external programs. With an instance of the java.lang.ProcessBuilder class, it can execute an external program and return an instance of a subclass of java.lang.Process. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

public class ProcessBuildDemo { 
    public static void main(String [] args) throws IOException {

        String[] command = {"CMD", "/C", "dir"};
        ProcessBuilder probuilder = new ProcessBuilder( command );
        //You can set up your work directory
        probuilder.directory(new File("c:\\xyzwsdemo"));

        Process process = probuilder.start();

        //Read out dir output
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        System.out.printf("Output of running %s is:\n",
                Arrays.toString(command));
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        //Wait to get exit value
        try {
            int exitValue = process.waitFor();
            System.out.println("\n\nExit Value is " + exitValue);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
2 of 4
3

use below code to execute process using java.

final List<String> commands = new ArrayList<String>();                
    commands.add("cmd");
    commands.add(input2); // second commandline argument 
    commands.add(input3);// third commandline agrument
    ProcessBuilder pb = new ProcessBuilder(commands);
Find elsewhere
🌐
Krishna Kumar
krishnakumar.hashnode.dev › a-beginners-guide-to-processbuilder-in-java
how processbuilder work in java - Krishna Kumar
February 4, 2023 - The most basic method is to use the start() method, which takes an array of strings as input. This array represents the command that you want to execute, along with any arguments.
🌐
Stack Overflow
stackoverflow.com › questions › 16749801 › execute-command-with-procesbuilder
java - Execute command with procesBuilder - Stack Overflow
ProcessBuilder builder = new ProcessBuilder( "sumo.exe", "-c", path + FILENAME_CONFIG + "\", "--netstate-dump", ouputPath); ... The command name should be sumo.exe. Unless sumo.exe is within the OS's search path, the OS will not be able to run it.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › processbuilder_command_string.htm
Java ProcessBuilder command() Method
The following example shows the usage of ProcessBuilder command() method. In this program, we've created a list of Strings and added calc.exe to it. Using that list, we've initialized a ProcessBuilder instance. Using command(list) method, we've added a list to the ProcessBuilder.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › ProcessBuilder.html
ProcessBuilder (Java SE 11 & JDK 11 )
January 20, 2026 - Modifying a process builder's ... or the Java process itself. Most error checking is performed by the start() method. It is possible to modify the state of an object so that start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless start() is invoked. Note that this class is not synchronized. If multiple threads access a ProcessBuilder instance ...
Top answer
1 of 1
8

"(actually it's "ls" but it should work exactly the same way)"

No, it is not. Because the 'ls' process returns immediately after its call. Your omixplayer at the other hand is interactive and will accept commands at runtime.

What you have to do:

  • create a class which implements Runnable and let this class read from the prs.getInputStream(). You will need this because the .read() will block and wait for new data to read.

  • get the OutputStream of the Process object (prs.getOutputStream()). Everything you write to the OutputStream will be read from your omixplayer. Don't forget to flush the OutputStream and every command needs an "\n" at the end to be executed.

Like that:

public class TestMain {
    public static void main(String a[]) throws InterruptedException {

        List<String> commands = new ArrayList<String>();
        commands.add("telnet");
        commands.add("www.google.com");
        commands.add("80");
        ProcessBuilder pb = new ProcessBuilder(commands);
        pb.redirectErrorStream(true);
        try {

            Process prs = pb.start();
            Thread inThread = new Thread(new In(prs.getInputStream()));
            inThread.start();
            Thread.sleep(2000);
            OutputStream writeTo = prs.getOutputStream();
            writeTo.write("oops\n".getBytes());
            writeTo.flush();
            writeTo.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class In implements Runnable {
    private InputStream is;

    public In(InputStream is) {
        this.is = is;
    }

    @Override
    public void run() {
        byte[] b = new byte[1024];
        int size = 0;
        try {
            while ((size = is.read(b)) != -1) {
                System.err.println(new String(b));
            }
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

P.S.: Keep in mind this example is quick an dirty.

🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 6)
Modifying a process builder's ... or the Java process itself. Most error checking is performed by the start() method. It is possible to modify the state of an object so that start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless start() is invoked. Note that this class is not synchronized. If multiple threads access a ProcessBuilder instance ...
🌐
Dot Net Perls
dotnetperls.com › process-java
Java - Process, ProcessBuilder Examples - Dot Net Perls
September 16, 2024 - import java.io.IOException; import java.lang.ProcessBuilder; public class Program { public static void main(String[] args) throws IOException { // Create ProcessBuilder. ProcessBuilder p = new ProcessBuilder(); // Use command "notepad.exe" and open the file. p.command("notepad.exe", ...