java调用linux命令行

参考博客:
Java调用Linux命令(cd的处理)

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CmdTestThree {
    public static void main(String[] args) {
        CmdTestThree cmdTestThree = new CmdTestThree();
        ArrayList<String> cmdList = new ArrayList<>();
//        cmdList.add("firefox");
        cmdList.add("cd /home/dj/test");
        cmdList.add("pwd");
        cmdList.add("touch 1.txt");
        cmdList.add("echo \"Assess\" > 1.txt");
        cmdList.add("touch 2.txt");
        cmdList.add("touch 3.txt");
        List<String> res = cmdTestThree.executeNewFlow(cmdList);
        System.out.println("resaaa:" + res);
    }
    public List<String> executeNewFlow(List<String> commands) {
        List<String> rspList = new ArrayList<String>();
        Runtime run = Runtime.getRuntime();
        try {
            Process proc = run.exec("/bin/bash", null, null);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            for (String line : commands) {
                out.println(line);
            }
            // out.println("cd /home/test");
            // out.println("pwd");
            // out.println("rm -fr /home/proxy.log");
            out.println("exit");// 这个命令必须执行,否则in流不结束。
            String rspLine = "";
            while ((rspLine = in.readLine()) != null) {
                System.out.println(rspLine);
                rspList.add(rspLine);
            }
            proc.waitFor();
            in.close();
            out.close();
            proc.destroy();
        } catch (IOException | InterruptedException e1) {
            e1.printStackTrace();
        }
        return rspList;
    }
}

你可以使用Java中的Runtime类或ProcessBuilder类来调用Linux命令行。 下面是使用Runtime类来执行Linux命令的示例代码: ```java try { // 执行 command 命令 String command = "ls -l"; Process process = Runtime.getRuntime().exec(command); // 获取命令输出结果 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("Exited with error code " + exitCode); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ``` 上面的代码将执行`ls -l`命令并输出结果。 使用ProcessBuilder类也可以达到同样的效果。以下是使用ProcessBuilder类执行Linux命令的示例代码: ```java try { // 执行 command 命令 ProcessBuilder processBuilder = new ProcessBuilder("ls", "-l"); 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("Exited with error code " + exitCode); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ``` 这里使用了ProcessBuilder类来创建一个进程,然后执行`ls -l`命令。通过进程的输入输出流来获取命令执行的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值