在 Java 中运行 Bash 命令

新手上路,请多包涵

我有以下课程。它允许我通过 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”目录的内容?

原文由 mickzer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
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;
}

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

如果脚本生成很复杂,使用类似 velocity 的模板引擎也可能是合理的。

编辑

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

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

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

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

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

原文由 René Link 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题