我怎么能运行在Java中只在一个CMD窗口多个命令? [英] How can I run multiple commands in just one cmd windows in Java?

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

问题描述

从Java应用程序

我想要做的是运行批量文件多次。因此,我成立了一个 for循环运行此code N 时间:

 的for(int i = 0; I< N;我++){
    。调用Runtime.getRuntime()EXEC(CMD / C somefile.bat启动);
}

的问题是,现在每个运行命令一个新的命令窗口时间弹出。然而,我要的只是有一个窗口,在开始弹出并用来显示来自以下命令调用的所有数据。

我怎么能这样做?


解决方案

&放大器;&安培; 您可以执行多个命令,一个接一个:

 调用Runtime.getRuntime()EXEC(CMD / C \\开始somefile.bat&放大器;&功放;启动other.bat和放大器;&安培; CD C:\\\\测试&放大器; &安培; TEST.EXE \\);


  

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


  
  

您可以使用条件处理符号运行一个命令行或脚本多个命令。当您运行使用条件处理符号的多个命令,这些命令以基于命令的条件处理符号左边的结果条件处理符号行为的权利。


  
  

例如,您可能需要运行仅在previous命令失败的命令。或者,您可能需要运行仅在previous命令成功执行的命令。
  您可以使用下表中列出的特殊字符来传递多个命令。


  
  

&安培; [...]命令1&放大器;命令2 结果
   使用多个命令在一个命令行中分离出来。 cmd.exe的运行第一命令,然后第二个命令。


  
  

&放大器;&安培; [...]命令1&放大器;&安培;命令2 结果
   使用运行命令以下和放大器;&安培;只有命令preceding符号是成功的。 CMD.EXE运行第一个命令,然后只运行在第一个命令成功完成第二个命令。


  
  

|| [...]命令1 ||命令2 结果
   使用运行命令以下||只有命令preceding ||失败。 CMD.EXE运行第一个命令,然后只运行在第一个命令没有成功完成第二个命令(接收到错误code大于零)。


  
  

()[...]按钮(Command&安培;命令2)结果
   使用分组或嵌套多个命令。


  
  

;或者,命令1参数1,参数2 结果
   用于分隔命令参数。


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天全站免登陆