简体   繁体   English

如何通过 Runtime.getRuntime().exec() 在命令行应用程序中执行命令

[英]How to execute commands in a command-line application through Runtime.getRuntime().exec()

I am trying to execute a third-party command-line application from Java.我正在尝试从 Java 执行第三方命令行应用程序。 The application is called CWB.该应用程序称为 CWB。 I'm using the following code:我正在使用以下代码:

process = Runtime.getRuntime().exec(script.sh);

LinkedList<String> stringList = new LinkedList<String>();

BufferedReader reader = new BufferedReader(
    new InputStreamReader(process.getInputStream()));

String line = "";

while ((line = reader.readLine()) != null) {
    stringList.add(line);
}

int exitVal = 0;

exitVal = process.waitFor();
if (exitVal == 0) {
    // Success
    return stringList;
} else {
    // Failure
    return null;
}

The commands are put in a file script.sh (I'm in Linux).这些命令放在文件script.sh (我在 Linux 中)。 The script contains the following two commands:该脚本包含以下两个命令:

# Run CWB application from Terminal
./cwb-nc-ccs-x86-linux.bin
# Execute a CWB-specific command
load file_name.ccs

The behavior is different when I execute CWB from the terminal and when I execute it in Java through Runtime.getRuntime().exec() .当我从终端执行 CWB 和我通过Runtime.getRuntime().exec()在 Java 中执行它时,行为是不同的。 When I execute CWB from Terminal, I obtain this output:当我从终端执行 CWB 时,我获得了这个输出:

CWB_output_from_terminal CWB_output_from_terminal

But, when I execute from Java through Runtime.getRuntime().exec() , I obtain this output in the console:但是,当我通过Runtime.getRuntime().exec()从 Java 执行时,我在控制台中获得了这个输出:

The Concurrency Workbench of the New Century
(Version 1.2 --- June, 2000)

That is, the shell in Java stops execution after the command ./cwb-nc-ccs-x86-linux.bin .也就是说,Java 中的 shell 在命令./cwb-nc-ccs-x86-linux.bin之后停止执行。 I think the reason is that, when I start the CWB process, it's like as if I enter in a new "environment".我想原因是,当我开始 CWB 流程时,就像我进入了一个新的“环境”。 It can be noticed because the cursor line where I type the commands starts with cwb-nc> , as we can see in the picture above.可以注意到,因为我键入命令的光标行以cwb-nc>开头, cwb-nc>图所示。 Thus, being in a new "environment" and not in the shell, Java doesn't know how to pass commands to this environment.因此,在一个新的“环境”中而不是在 shell 中,Java 不知道如何将命令传递给这个环境。 Am I right?我对吗? How can I solve this problem and execute the other command in the script ( load file_name.ccs )?如何解决此问题并执行脚本中的其他命令( load file_name.ccs )?

I actually had a similar problem when using Lilypond in my Java Application.在我的 Java 应用程序中使用 Lilypond 时,我实际上遇到了类似的问题。 I believe you are accurate in your conclusion about how the Java command works.我相信您对 Java 命令如何工作的结论是准确的。 It is not equivalent to typing in the command shell, so there are some special preparations we have to make in order to get it to do what we want.它不等同于在命令 shell 中键入,因此我们必须做一些特殊的准备才能让它执行我们想要的操作。

I had to create a script file and then fool around with the execute command until I found one that actually brought up the command window and executed the commands correctly:我必须创建一个脚本文件,然后在执行命令中鬼混,直到我找到一个实际打开命令窗口并正确执行命令的文件:

//Run the script
            String command = "cmd.exe /C \"C:/Users/Lindsay Gen10/Documents/NetBeansProjects/capstone/src/main/resources/templates/pdfs/" 
                    + song.getTitle().replaceAll(" ", "") + "Script.sh\"";
            Runtime.getRuntime().exec("cmd /c run cmd.exe /K ");
            Process p = Runtime.getRuntime().exec(command);
            p.waitFor();

By using the cmd /c run , it actually forces a command window to open.通过使用cmd /c run ,它实际上会强制打开一个命令窗口。 I'm not 100% sure this will solve your problem too, but it's worth a try!我不是 100% 确定这也能解决您的问题,但值得一试!

If you had tried to run your script outside of Java, you'd see that it doesn't work there either.如果您曾尝试在 Java 之外运行您的脚本,您会发现它在那里也不起作用。 You should focus on writing a script that works first, and only try invoking it from Java when it does.您应该专注于编写一个首先起作用的脚本,并且只有在它起作用时才尝试从 Java 调用它。

The problem with the script is detailed here: Pass commands as input to another command (su, ssh, sh, etc) .此处详细介绍了脚本的问题:将命令作为输入传递给另一个命令(su、ssh、sh 等)

However, since you're running from Java, you could and probably should implement this purely in Java without a shell script:但是,由于您是从 Java 运行的,因此您可以并且可能应该在没有 shell 脚本的情况下完全在 Java 中实现它:

process = Runtime.getRuntime().exec(new String[] { "./cwb-nc-ccs-x86-linux.bin" });
PrintStream writer = new PrintStream(process.getOutputStream());
writer.println("load file_name.ccs");
writer.close();
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过java Runtime.getRuntime()。exec()执行CURL命令 - Unable to execute CURL command through java Runtime.getRuntime().exec() 如何使Runtime.getRuntime()。exec一一执行命令并获取输出,而不是执行所有命令 - How to make Runtime.getRuntime().exec execute command one by one and get output rather than executing all commands Runtime.getRuntime()。exec()不执行某些命令 - Runtime.getRuntime().exec() doesn't execute some commands 为什么 Runtime.getRuntime().exec(startupOracle); 没有完全执行命令 - why Runtime.getRuntime().exec(startupOracle); does not fully execute command 通过Runtime.getRuntime()。exec运行linux命令时出错 - Error while running linux command through Runtime.getRuntime().exec 通过Runtime.getRuntime()。exec()执行查找命令时出错 - Error executing find command through Runtime.getRuntime().exec() 使用“ Runtime.getRuntime()。exec()”执行命令行 - Executing command line using “Runtime.getRuntime().exec()” Runtime.getRuntime()。exec如何运行goto命令 - Runtime.getRuntime().exec how to run goto commands 如何通过java获取命令“top -c -b -n 1”的完整结果,Runtime.getRuntime().exec(command) - How to get the complete result of command "top -c -b -n 1" through java, Runtime.getRuntime().exec(command) 如何使用Runtime.getRuntime()。exec()执行ant并关闭命令提示符 - How to execute ant and close the command prompt using Runtime.getRuntime().exec()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM