如何在 Java 的一个 cmd 窗口中运行多个命令? [英] How can I run multiple commands in just one cmd windows in Java?

查看:35
本文介绍了如何在 Java 的一个 cmd 窗口中运行多个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从 Java 应用程序多次运行 batch 文件.因此,我设置了一个 for-loop 运行此代码 n 次:

for (int i = 0; i 

问题是现在每次运行命令时都会弹出一个新的 cmd 窗口.但是,我想要的只是 一个 窗口,它在开头弹出,用于显示来自以下命令调用的所有数据.

我该怎么做?

解决方案

使用 && 你可以执行多个命令,一个接一个:

Runtime.getRuntime().exec("cmd/c "start somefile.bat && start other.bat && cd C:\test && test.exe"");

<块引用>

使用多个命令和条件处理符号

您可以使用条件处理符号从单个命令行或脚本运行多个命令.当您使用条件处理符号运行多个命令时,条件处理符号右侧的命令将根据条件处理符号左侧的命令的结果执行操作.

例如,您可能只想在前一个命令失败时运行命令.或者,您可能只想在前一个命令成功时运行命令.您可以使用下表中列出的特殊字符来传递多个命令.

<代码>&[...] command1 &命令2
用于在一个命令行上分隔多个命令.Cmd.exe 运行第一个命令,然后是第二个命令.

<代码>&&[...] command1 &&命令2
用于运行 && 后面的命令仅当符号前面的命令成功时.Cmd.exe 运行第一个命令,然后仅当第一个命令成功完成时才运行第二个命令.

<代码>||[...] command1 ||命令2
用于运行 || 后面的命令仅当 || 前面的命令失败.Cmd.exe 运行第一个命令,然后仅在第一个命令未成功完成(收到大于零的错误代码)时才运行第二个命令.

( ) [...] (command1 & command2)
用于分组或嵌套多个命令.

<代码>;或 , command1 parameter1;parameter2
用于分隔命令参数.

what I would like to do is run a batch file multiple times from a java application. Therefore I set up a for-loop that runs this code n times:

for (int i = 0; i < n; i++) {
    Runtime.getRuntime().exec("cmd /c start somefile.bat");
}

The problem is that now each time the command is run a new cmd window pops up. However, what I want is just one window that pops up at the beginning and that is used to display all data from the following command calls.

How can I do that?

解决方案

With && you can execute more than one commands, one after another:

Runtime.getRuntime().exec("cmd /c "start somefile.bat && start other.bat && cd C:\test && test.exe"");

Using multiple commands and conditional processing symbols

You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol.

For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful. You can use the special characters listed in the following table to pass multiple commands.

& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

|| [...] command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

( ) [...] (command1 & command2)
Use to group or nest multiple commands.

; or , command1 parameter1;parameter2
Use to separate command parameters.

这篇关于如何在 Java 的一个 cmd 窗口中运行多个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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