CommandUtil.java
package com.lanying.util;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class CommandUtil {
/**
* 在指定路径下执行一条命令,不能执行cd之类的命令
*
* @param command 要执行的Linux命令
* @param dir 目标路径,在该路径执行上述Linux命令
* @return 命令执行后显示的结果
* @throws IOException
*/
public static String run(String command, File dir) throws IOException {
Scanner input = null;
StringBuilder result = new StringBuilder(command + "\n"); // 加上命令本身
Process process = null;
try {
process = Runtime.getRuntime().exec(command, null, dir);
try {
//等待命令执行完成
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result.append(input.nextLine() + "\n");
}
} finally {
if (input != null) {
input.close();
}
if (process != null) {
process.destroy();
}
}
return result.toString();
}
/**
* 在指定路径下,开启脚本,可以执行多条命令
*
* @param command 要执行的Linux命令
* @param dir 目标路径,在该路径执行上述Linux命令
* @return 所有命令执行后显示的结果
* @throws IOException
*/
public static List<String> run(String[] command, File dir) throws IOException {
BufferedReader in = null;
PrintWriter out = null;
List<String> resultList = new ArrayList<String>();
Process process = null;
try {
// 开启脚本是为了能够获取所有的返回结果
process = Runtime.getRuntime().exec("/bin/sh", null, dir); // /bin/sh开启shell脚本
in = new BufferedReader(new InputStreamReader(process.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true);
for (String cmd : command) {
out.println(cmd); // 一条条执行命令
}
out.println("exit"); // 表示脚本结束,必须执行,否则in流不结束。
try {
// 等待命令执行完成
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 装填返回结果,比如ls命令执行后显示的一行行字符串
String line = "";
while ((line = in.readLine()) != null) {
resultList.add(line);
}
resultList.add(0, Arrays.toString(command)); // 加上命令本身,可根据需要自行取舍
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (process != null) {
process.destroy();
}
}
return resultList;
}
}
JUnitRuntimeExec.java
package com.lanying.demo;
import com.lanying.util.CommandUtil;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JUnitRuntimeExec {
@Test
public void testCMD(){
File dir = new File("/lanying");
try {
String res = CommandUtil.run("ls -l",dir);
System.out.println(res);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testCMDList(){
// 在该目录下执行命令
File dir = new File("/lanying");
List<String> cmdList = new ArrayList<>();
cmdList.add("ls -lrt");
cmdList.add("ls -lrt");
try {
// 执行命令
List<String> res = CommandUtil.run(cmdList.toArray(new String[cmdList.size()]),dir);
// 遍历结果
res.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行效果:
Java程序执行单条Linux命令效果图.png
Java程序执行多条Linux命令效果图.png