How To Run a Command using Java in Linux or Windows

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 accepts a command and arguments in an array. Here’s an example.

Executing a Command using ProcessBuilder

Below is a sample code on how to execute or run a command using Java. First, we create a new ProcessBuilder and add the command.

Next, we start the process using the start() method.

Then, we read the output by getting the InputStream from the process and putting it inside the BufferedReader.

Wait for the process to complete by invoking the waitFor() method.

Lastly, we close the resources and destroy the process.

    public void runCommand(String... command) {
        ProcessBuilder processBuilder = new ProcessBuilder().command(command);

        try {
            Process process = processBuilder.start();

            //read the output
            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String output = null;
            while ((output = bufferedReader.readLine()) != null) {

            //wait for the process to complete

            //close the resources

        } catch (IOException | InterruptedException e) {

Testing in Windows Machine

Let’s try to test it in Windows by executing commands. Take note that the “cd”, “dir”, or “copy” commands are not executable by itself. They are part of cmd.exe command. Therefore, we need to call in first the cmd and pass the commands.

Other commands are executable by itself eg. “ipconfig”, “java”, etc.

    public static void main(String... args) {
        Main main = new Main();

        //test windows commands
        //in windows, cd, dir, copy, etc. are not executable commands
        //they are part of the windows command interpreter cmd.exe
        //therefore we need to call cmd.exe with option /C then our command to execute
        main.runCommand("cmd", "/C", "dir");
        main.runCommand("ipconfig", "/all");

And this will show you the output of listing all the files and directories in your working directory and the network configuration with the ipconfig command.

Testing in Linux Machine

Now, let’s try to test it in Linux Machine. As mostly the commands in Linux are already available in bash, we don’t need to add “bash -c” in our commands. In any case that your command is not working, try adding “bash”, “-c” first in your command array argument.

    public static void main(String... args) {
        Main main = new Main();

        //test command in linux
        main.runCommand("ifconfig", "-a");

Here, we try to print the current working directory and the network configurations available. Here’s a sample output:

You can even run a shell script by just putting the script file location and starting with a forward slash eg., “/home/”.

That’s it for this guide. You have learned how to execute or run a command using Java in Windows or Linux systems. Let us know in the comments if this guide helps you. Also, you might want to visit our tutorial on how to convert images to base64 in Java.

Share this tutorial!