Java调用shell脚本或命令时,可使用下面的两种方式:
1、Runtime.getRuntime().exec();
2、通过ProcessBuilder进行调度。
第一种方式比较直接,具体使用方式见https://blog.csdn.net/arkblue/article/details/7897396
由于在使用过程中,涉及到shell命令的字符串和数组形式,以及权限问题、同时调用多条shell命令等,这时候第二种方式就比较好用了,具体使用方式和代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.callShellCommand(new String[] { "ipconfig" }, null);
// 命令中有多个部分时,把命令中的每个部分写成以逗号分隔的字符串形式
// test.callShellCommand(new String[] { "part1", "part2", "part3", ...}, null