如何从Java执行Shell命令
作者:互联网
在Java中,我们可以使用ProcessBuilder
或Runtime.getRuntime().exec
执行外部shell命令:
1. ProcessBuilder
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProcessBuilderExample1 {
public static void main(String[] args) {
ProcessBuilder processBuilder = new ProcessBuilder();
// Windows
processBuilder.command("cmd.exe", "/c", "ping -n 3 bing.com");
try {
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2. Runtime.getRuntime().exec()
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecuteShellComand {
public static void main(String[] args) {
try {
// -- Linux --
// Run a shell command
// Process process = Runtime.getRuntime().exec("ls /home/user/");
// Run a shell script
// Process process = Runtime.getRuntime().exec("path/to/hello.sh");
// -- Windows --
// Run a command
//Process process = Runtime.getRuntime().exec("cmd /c dir C:\\Users\\Administrator");
//Run a bat file
Process process = Runtime.getRuntime().exec(
"cmd /c hello.bat", null, new File("C:\\Users\\Administrator\\"));
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
//abnormal...
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
标签:Shell,Java,process,getRuntime,new,命令,io,import,java 来源: https://blog.csdn.net/allway2/article/details/112208119