在 Java 中运行 Bash 命令

2022-09-01 09:44:50

我有以下课程。它允许我通过java执行命令。

public class ExecuteShellCommand {

public String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

}

}

当我运行命令时,不会保存上一个命令的结果。例如:

public static void main(String args[]) {

    ExecuteShellCommand com = new ExecuteShellCommand();
    System.out.println(com.executeCommand("ls"));
    System.out.println(com.executeCommand("cd bin"));
    System.out.println(com.executeCommand("ls"));

}

给出输出:

bin
src


bin
src

为什么第二个“ls”命令不显示“bin”目录的内容?


答案 1

使用 启动新进程。每个进程都有一个工作目录。这通常是启动父进程的目录,但您可以更改启动进程的目录。Runtime.exec(command)

我建议使用 ProcessBuilder

ProcessBuilder pb = new ProcessBuilder("ls");
pb.inheritIO();
pb.directory(new File("bin"));
pb.start();

如果要在 shell 中运行多个命令,最好创建一个临时 shell 脚本并运行此脚本。

public void executeCommands() throws IOException {

    File tempScript = createTempScript();

    try {
        ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString());
        pb.inheritIO();
        Process process = pb.start();
        process.waitFor();
    } finally {
        tempScript.delete();
    }
}

public File createTempScript() throws IOException {
    File tempScript = File.createTempFile("script", null);

    Writer streamWriter = new OutputStreamWriter(new FileOutputStream(
            tempScript));
    PrintWriter printWriter = new PrintWriter(streamWriter);

    printWriter.println("#!/bin/bash");
    printWriter.println("cd bin");
    printWriter.println("ls");

    printWriter.close();

    return tempScript;
}

当然,您也可以在系统上使用任何其他脚本。在运行时生成脚本有时是有意义的,例如,如果执行的命令必须更改。但是,您应该首先尝试创建一个可以使用参数调用的脚本,而不是在运行时动态生成它。

如果脚本生成很复杂,则使用速度等模板引擎也可能是合理的。

编辑

您还应该考虑将流程构建器的复杂性隐藏在简单的界面后面。

将您需要的内容(接口)与完成方式(实现)分开。

public interface FileUtils {
    public String[] listFiles(String dirpath);
}

然后,您可以提供使用流程构建器或本机方法来完成工作的实现,并且可以为不同的环境(如Linux或Windows)提供不同的实现。

最后,这样的接口在单元测试中也更容易模拟。


答案 2

你可以形成一个复杂的 bash 命令来完成所有操作:“ls;cd bin;ls”。要使这项工作,您需要显式调用bash。这种方法应该为您提供bash命令行的所有功能(报价处理,$扩展,管道等)。

/**
 * Execute a bash command. We can handle complex bash commands including
 * multiple executions (; | && ||), quotes, expansions ($), escapes (\), e.g.:
 *     "cd /abc/def; mv ghi 'older ghi '$(whoami)"
 * @param command
 * @return true if bash got started, but your command may have failed.
 */
public static boolean executeBashCommand(String command) {
    boolean success = false;
    System.out.println("Executing BASH command:\n   " + command);
    Runtime r = Runtime.getRuntime();
    // Use bash -c so we can handle things like multi commands separated by ; and
    // things like quotes, $, |, and \. My tests show that command comes as
    // one argument to bash, so we do not need to quote it to make it one thing.
    // Also, exec may object if it does not have an executable file as the first thing,
    // so having bash here makes it happy provided bash is installed and in path.
    String[] commands = {"bash", "-c", command};
    try {
        Process p = r.exec(commands);

        p.waitFor();
        BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";

        while ((line = b.readLine()) != null) {
            System.out.println(line);
        }

        b.close();
        success = true;
    } catch (Exception e) {
        System.err.println("Failed to execute bash with command: " + command);
        e.printStackTrace();
    }
    return success;
}

推荐