import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ShellCommandExample {
public static void main(String[] args) {
try {
// Command to execute (e.g., listing files in a directory)
String command = "ls -l";
// Create ProcessBuilder
ProcessBuilder processBuilder = new ProcessBuilder();
// Set the command to execute
processBuilder.command("bash", "-c", command);
// Start the process
Process process = processBuilder.start();
// Read the output of the command
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 (Exception e) {
e.printStackTrace();
}
}
}