🌐
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 ...
🌐
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.
🌐
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.
🌐
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:
🌐
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.
🌐
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 −
🌐
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( ...
🌐
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.
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
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 7 )
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.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
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.
🌐
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 - For example, we can create a process builder for each isolated command and compose them into the pipeline: @Test public void givenProcessBuilder_whenStartingPipeline_thenSuccess() throws IOException, InterruptedException { List<ProcessBuilder> builders = Arrays.asList( new ProcessBuilder("find", "src", "-name", "*.java", "-type", "f"), new ProcessBuilder("wc", "-l")); List<Process> processes = ProcessBuilder.startPipeline(builders); Process last = processes.get(processes.size() - 1); List<String> output = readOutput(last.getInputStream()); assertThat("Results should not be empty", output, is(not(empty()))); }
🌐
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); }
🌐
JavaPointers
javapointers.com › java › java-core › how-to-run-a-command-using-java-in-linux-or-windows
How To Run a Command using Java in Linux or Windows - JavaPointers
May 24, 2020 - In this guide, we will show you how to run a command using Java in Linux or Windows machine. This will execute commands such as in command prompt in Windows or bash in Linux. The Process API in Java lets you execute commands in the System. For example, printing the current directory, showing the network interfaces, and more. The ProcessBuilder ...