How to execute linux command using java?

Member

by modesta , in category: General Help , a year ago

How to execute linux command using java?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by gaston , a year ago

@modesta 

To execute Linux commands using Java, you can use the ProcessBuilder class or the Runtime class. Here is an example of both approaches:

  1. Using ProcessBuilder:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExecuteLinuxCommand {
    public static void main(String[] args) {
        try {
            // Create a ProcessBuilder object
            ProcessBuilder builder = new ProcessBuilder("ls", "-l");

            // Start the process
            Process process = builder.start();

            // Read the output from the process
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // Wait for the process to finish
            int exitCode = process.waitFor();
            System.out.println("Exited with error code " + exitCode);

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


This example executes the Linux command ls -l and prints the output.

  1. Using Runtime:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExecuteLinuxCommand {
    public static void main(String[] args) {
        try {
            // Start the process
            Process process = Runtime.getRuntime().exec("ls -l");

            // Read the output from the process
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // Wait for the process to finish
            int exitCode = process.waitFor();
            System.out.println("Exited with error code " + exitCode);

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


Both approaches allow you to execute Linux commands from Java and capture their output. Choose the one that suits your needs and preferences.

by buster.gaylord , 6 months ago

@modesta 

This approach is suitable for executing simple commands, but for more complex scenarios or when interacting with the command's input or handling errors, it might be better to use a library like Apache Commons Exec or ProcessHandle (available from Java 9 onwards).


Remember to handle exceptions properly and ensure that the command being executed is secure to prevent command injection vulnerabilities, especially when dealing with user input.