Чтобы запустить команду оболочки в Java, существует несколько способов. Вот несколько примеров:
Метод 1: использование Runtime.getRuntime().exec()
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ShellCommandRunner {
public static void main(String[] args) {
try {
String command = "ls -l"; // Replace with your desired shell command
Process process = Runtime.getRuntime().exec(command);
// 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 command to finish execution
int exitCode = process.waitFor();
System.out.println("Command exited with code: " + exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Метод 2: использование ProcessBuilder
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ShellCommandRunner {
public static void main(String[] args) {
try {
String command = "ls -l"; // Replace with your desired shell command
ProcessBuilder processBuilder = new ProcessBuilder(command.split("\\s+"));
// Redirect the output to the Java process
processBuilder.redirectErrorStream(true);
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 command to finish execution
int exitCode = process.waitFor();
System.out.println("Command exited with code: " + exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Метод 3: использование библиотеки Apache Commons Exec
Вы также можете использовать библиотеку Apache Commons Exec для запуска команд оболочки на Java. Вот пример:
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteResultHandler;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ShellCommandRunner {
public static void main(String[] args) {
try {
String command = "ls -l"; // Replace with your desired shell command
CommandLine cmdLine = CommandLine.parse(command);
// Create an executor with timeout
Executor executor = new DefaultExecutor();
executor.setExitValue(0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(10));
executor.setWatchdog(watchdog);
// Capture the output of the command
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
executor.setStreamHandler(streamHandler);
// Execute the command asynchronously
executor.execute(cmdLine, new ExecuteResultHandler() {
@Override
public void onProcessComplete(int exitValue) {
System.out.println("Command exited with code: " + exitValue);
}
@Override
public void onProcessFailed(ExecuteException e) {
System.out.println("Command execution failed: " + e.getMessage());
}
});
// Wait for the command to finish execution
watchdog.waitFor();
// Print the output
System.out.println(outputStream.toString());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}