Java - 运行Bash命令 [英] Java - Running Bash Commands

查看:125
本文介绍了Java - 运行Bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following class. It allows me to execute commands through 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();

}

}

当我跑步时命令,不保存上一个命令的结果。例如:

When I run commands, the result of the previous command isn't saved. For example:

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'目录的内容?

Why doesn't the second 'ls' command show the contents of the 'bin' directory?

推荐答案

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

You start a new process with Runtime.exec(command). Each process has a working directory. This is normally the directory in which the parent process was started, but you can change the directory in which your process is started.

我建议使用 ProcessBuilder

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

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

If you want to run multiple commands in a shell it would be better to create a temporary shell script and run this.

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;
}

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

Of course you can also use any other script on your system. Generating a script at runtime makes sometimes sense, e.g. if the commands that are executed have to change. But you should first try to create one script that you can call with arguments instead of generating it dynamically at runtime.

使用像velocity这样的模板引擎也是合理的如果脚本生成很复杂。

It might also be reasonable to use a template engine like velocity if the script generation is complex.

这篇关于Java - 运行Bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆