Open In App

Java Runtime exec() Method with Examples

The Runtime class’s exec() method is your key to making your Java application interact with the operating system, if you’ve ever wanted it to. In this post, we’ll examine the exec() method in great detail, dissect its fundamental ideas, and highlight all of its different applications.

Java Runtime exec() Method

Imagine you’re running a Java program, and you need to execute a system command. This could be anything from launching an external program to managing system processes. The exec() method is your gateway to this world of possibilities.



Here’s a simple way to think about it: You’re the conductor of a Java orchestra, and the exec() method is your baton. With it, you can instruct the OS to perform tasks outside the realm of Java, seamlessly integrating your application with the underlying system. So let’s get into the details.

Understanding the Core Concepts

Before we jump into the practical examples, let’s cover some essential concepts related to the exec() method:



Exploring different variants of exec() method

The exec() method has six variants, each tailored to different scenarios:

Runtime.getRuntime().exec(“notepad.exe”);

String[] cmd = {“ls”, “-l”, “/path/to/directory”};

Runtime.getRuntime().exec(cmd);

Let’s Dive into Examples : Now, let’s get our hands dirty with some interactive examples:




// Java program to demonstrate
// Java Runtime exec() method
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
  
//Driver class
public class GfgExecDemo {
  //main function
  public static void main(String[] args) {
        try {
            // Variant 1: exec(String command)
            // This variant executes the "notepad.exe" command, which opens Notepad.
            Process process1 = Runtime.getRuntime().exec("notepad.exe");
            int exitCode1 = process1.waitFor();
            System.out.println("Variant 1 - Exit Code: " + exitCode1);
  
            // Variant 2: exec(String[] cmdarray)
            // This variant executes the "ls -l /path/to/directory" command, listing files in a directory.
            String[] cmd2 = {"ls", "-l", "/path/to/directory"};
            Process process2 = Runtime.getRuntime().exec(cmd2);
            int exitCode2 = process2.waitFor();
            System.out.println("Variant 2 - Exit Code: " + exitCode2);
  
            // Variant 3: exec(String[] cmdarray, String[] envp)
            // This variant executes the "echo $HOME" command with a custom environment variable.
            String[] cmd3 = {"echo", "$HOME"};
            String[] env3 = {"HOME=/path/to/custom/home"};
            Process process3 = Runtime.getRuntime().exec(cmd3, env3);
            int exitCode3 = process3.waitFor();
            System.out.println("Variant 3 - Exit Code: " + exitCode3);
  
            // Variant 4: exec(String[] cmdarray, String[] envp, File dir)
            // This variant executes the "ls -l" command with a custom 
            // environment variable and working directory.
            String[] cmd4 = {"ls", "-l"};
            String[] env4 = {"HOME=/path/to/custom/home"};
            File workingDir4 = new File("/path/to/directory");
            Process process4 = Runtime.getRuntime().exec(cmd4, env4, workingDir4);
            int exitCode4 = process4.waitFor();
            System.out.println("Variant 4 - Exit Code: " + exitCode4);
  
            // Variant 5: exec(String command, String[] envp)
            // This variant executes the "echo $HOME" command with a custom environment variable.
            String cmd5 = "echo $HOME";
            String[] env5 = {"HOME=/path/to/custom/home"};
            Process process5 = Runtime.getRuntime().exec(cmd5, env5);
            int exitCode5 = process5.waitFor();
            System.out.println("Variant 5 - Exit Code: " + exitCode5);
  
            // Variant 6: exec(String command, String[] envp, File dir)
            // This variant executes the "ls -l" command with a custom environment 
            // variable and working directory.
            String cmd6 = "ls -l";
            String[] env6 = {"HOME=/path/to/custom/home"};
            File workingDir6 = new File("/path/to/directory");
            Process process6 = Runtime.getRuntime().exec(cmd6, env6, workingDir6);
            int exitCode6 = process6.waitFor();
            System.out.println("Variant 6 - Exit Code: " + exitCode6);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Output :

Variant 1 - Exit Code: 0
Variant 2 - Exit Code: 0
Variant 3 - Exit Code: 0
Variant 4 - Exit Code: 0
Variant 5 - Exit Code: 0
Variant 6 - Exit Code: 0

The exec() function in Java’s Runtime class provides various options for communicating with the operating system. Once you understand this, you can easily integrate system commands into your Java programs. Remember to choose the option that best suits your needs. You can then explore process execution possibilities with confidence.


Article Tags :