天天看点

java执行多条linux命令_工具类:在Java程序中执行Linux命令

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 {

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();

}

public static List run(String[] command, File dir) throws IOException {

BufferedReader in = null;

PrintWriter out = null;

List resultList = new ArrayList();

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 cmdList = new ArrayList<>();

cmdList.add("ls -lrt");

cmdList.add("ls -lrt");

try {

// 执行命令

List res = CommandUtil.run(cmdList.toArray(new String[cmdList.size()]),dir);

// 遍历结果

res.forEach(System.out::println);

} catch (IOException e) {

e.printStackTrace();

}

}

}

执行效果:

java执行多条linux命令_工具类:在Java程序中执行Linux命令

Java程序执行单条Linux命令效果图.png

java执行多条linux命令_工具类:在Java程序中执行Linux命令

Java程序执行多条Linux命令效果图.png

参考资料:

本文地址:https://blog.csdn.net/lanying100/article/details/107352792

希望与广大网友互动??

点此进行留言吧!