🌐
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 ...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › processbuilder_command_string.htm
Java ProcessBuilder command() Method
package com.tutorialspoint; public class ProcessBuilderDemo { public static void main(String[] args) { // create a new list of arguments for our process String[] list = {"notepad.exe", "test.txt"}; // create the process builder ProcessBuilder pb = new ProcessBuilder(list); // set the command list pb.command(list); // print the new command list System.out.println(pb.command()); } } Let us compile and run the above program, this will produce the following result −
🌐
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 - Beginning with the end in mind, let's assume that we want to run the following Unix/Linux command from within a Java application: ... To run that command from a Java application using my new code, we'd first build and then exec the command like this:
🌐
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 - In this example, we create a ProcessBuilder object with the command ls, set the working directory to /tmp, and redirect the output to the standard output stream of the Java process. We then start the new process and wait for it to finish using the waitFor method. Finally, we print the exit code of the process to the console. In conclusion, the ProcessBuilder class provides a simple and powerful way to run commands from a Java process.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 8 )
April 21, 2026 - Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes.
🌐
ZetCode
zetcode.com › java › processbuilder
Java ProcessBuilder - creating processes in Java
May 1, 2025 - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; void main() throws IOException { var processBuilder = new ProcessBuilder(); processBuilder.command("cal", "2022", "-m 2"); var process = processBuilder.start(); try (var reader = new BufferedReader( ...
🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.processbuilder api
Guide to java.lang.ProcessBuilder API | Baeldung
November 26, 2025 - Process process = new ProcessBuilder("java", "-version").start(); First, we create our ProcessBuilder object passing the command and argument values to the constructor.
🌐
Thedeveloperblog
thedeveloperblog.com › processbuilder
Java ProcessBuilder Examples: Start Process, EXE
This program creates an instance of ProcessBuilder. It then calls command() to set the command. We use two arguments: two strings. Arguments: We pass a variable number of arguments to command—it combines these strings into a command string. Start: With start we invoke the command.
Find elsewhere
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);
🌐
Krishna Kumar
krishnakumar.hashnode.dev › a-beginners-guide-to-processbuilder-in-java
how processbuilder work in java - Krishna Kumar
February 19, 2023 - In this example, the command "java -version" is passed as an array of strings to the ProcessBuilder constructor. The start() method is then used to start the process. In the real world, we will probably want to capture the results of our running ...
🌐
Medium
beknazarsuranchiyev.medium.com › run-terminal-commands-from-java-da4be2b1dc09
Run terminal commands from Java. In this article, we will discuss how to… | by Beknazar | Medium
April 24, 2022 - You can modify the path of the file to point to your desktop or any folder and try running other commands. If you are on a Windows machine of course you have to run the command that works on the Windows command prompt. I have used ProcessBuilder to get an instance of Process class. ProcessBuilder is used to create operating system processes.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-processbuilder-class-java
Java.lang.ProcessBuilder class in Java - GeeksforGeeks
January 14, 2022 - Before JDK 5.0, the only way to create a process and execute it was to use Runtime.exec() method. It extends the class Object. This class is not synchronized. ... ProcessBuilder(List command): This constructs a process builder with the specified operating system program and arguments.
🌐
DZone
dzone.com › coding › java › running a java class as a subprocess
Running a Java Class as a Subprocess
May 21, 2019 - Allowing you to reuse this piece of code in several places while providing the flexibility to configure the IO redirects as well as the power to decide whether to run the subprocess in the background or block and wait for it to finish. This would look something like: public static ProcessBuilder exec(Class clazz, List<String> jvmArgs, List<String> args) { String javaHome = System.getProperty("java.home"); String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; String classpath = System.getProperty("java.class.path"); String className = clazz.getName(); List<String> command = new ArrayList<>(); command.add(javaBin); command.addAll(jvmArgs); command.add("-cp"); command.add(classpath); command.add(className); command.addAll(args); return new ProcessBuilder(command); }
🌐
Stack Abuse
stackabuse.com › executing-shell-commands-with-java
Executing Shell Commands with Java
May 18, 2020 - We've used the Runtime and ProcessBuilder classes to do this. Using Java, we can run single or multiple shell commands, execute shell scripts, run the terminal/command prompt, set working directories and manipulate environment variables through core classes.
🌐
Dot Net Perls
dotnetperls.com › process-java
Java - Process, ProcessBuilder Examples - Dot Net Perls
September 16, 2024 - ProcessBuilder p = new ProcessBuilder(); // Use command "notepad.exe" and open the file. p.command("notepad.exe", "C:\\file.txt"); p.start(); } } In this example we invoke a specific executable at a known location on the computer. We concat a folder path and an executable name.