[英]How can I run multiple commands in just one cmd windows in Java?
我想要做的是從 Java 應用程序多次運行batch
文件。 因此,我設置了一個運行此代碼n
次的for-loop
:
for (int i = 0; i < n; i++) {
Runtime.getRuntime().exec("cmd /c start somefile.bat");
}
問題是現在每次運行命令時都會彈出一個新的 cmd 窗口。 但是,我想要的只是一個在開頭彈出的窗口,用於顯示來自以下命令調用的所有數據。
我怎樣才能做到這一點?
使用&&您可以執行多個命令,一個接一個:
Runtime.getRuntime().exec("cmd /c \"start somefile.bat && start other.bat && cd C:\\test && test.exe\"");
使用多個命令和條件處理符號
您可以使用條件處理符號從單個命令行或腳本運行多個命令。 當您使用條件處理符號運行多個命令時,條件處理符號右側的命令將根據條件處理符號左側的命令的結果執行操作。
例如,您可能只想在前一個命令失敗時運行命令。 或者,您可能只想在前一個命令成功時運行命令。 您可以使用下表中列出的特殊字符來傳遞多個命令。
& [...] command1 & command2
用於在一個命令行上分隔多個命令。 Cmd.exe 運行第一個命令,然后是第二個命令。
&& [...] command1 && command2
僅當符號前面的命令成功時才用於運行 && 后面的命令。 Cmd.exe 運行第一個命令,然后僅當第一個命令成功完成時才運行第二個命令。
|| [...] command1 || command2
用於運行 || 后面的命令僅當 || 前面的命令失敗。 Cmd.exe 運行第一個命令,然后僅當第一個命令未成功完成(收到大於零的錯誤代碼)時才運行第二個命令。
( ) [...] (command1 & command2)
用於分組或嵌套多個命令。
; or , command1 parameter1;parameter2
用於分隔命令參數。
我會使用 Java 的ProcessBuilder或其他模擬/使用 shell 的類。 下面的代碼片段演示了這個想法(對於帶有 bash 的 Linux)。
import java.util.Scanner;
import java.io.*;
public class MyExec {
public static void main(String[] args)
{
//init shell
ProcessBuilder builder = new ProcessBuilder( "/bin/bash" );
Process p=null;
try {
p = builder.start();
}
catch (IOException e) {
System.out.println(e);
}
//get stdin of shell
BufferedWriter p_stdin =
new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
// execute the desired command (here: ls) n times
int n=10;
for (int i=0; i<n; i++) {
try {
//single execution
p_stdin.write("ls");
p_stdin.newLine();
p_stdin.flush();
}
catch (IOException e) {
System.out.println(e);
}
}
// finally close the shell by execution exit command
try {
p_stdin.write("exit");
p_stdin.newLine();
p_stdin.flush();
}
catch (IOException e) {
System.out.println(e);
}
// write stdout of shell (=output of all commands)
Scanner s = new Scanner( p.getInputStream() );
while (s.hasNext())
{
System.out.println( s.next() );
}
s.close();
}
}
請注意,它只是一個片段,需要針對 Windows 進行調整,但通常它應該與cmd.exe
。
@Jost 我很抱歉,但我不允許發表評論。 我還需要50個聲望。 您提供的解決方案是完美的解決方案。 我找了很久。 我只是為 Windows 編寫了一些測試類,everythink 可以完美運行。 非常感謝。
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Main {
private BufferedWriter p_stdin;
public Main() {
// init shell
ProcessBuilder builder = new ProcessBuilder("C:/Windows/System32/cmd.exe");
Process p = null;
try {
p = builder.start();
} catch (IOException e) {
System.out.println(e);
}
// get stdin of shell
p_stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
// execute commands
executeCommand("@echo off");
executeCommand("cd rsc");
executeCommand("exit");
// write stdout of shell (=output of all commands)
Scanner s = new Scanner(p.getInputStream());
while (s.hasNext()) {
System.out.println(s.next());
}
s.close();
}
private void executeCommand(String command) {
try {
// single execution
p_stdin.write(command);
p_stdin.newLine();
p_stdin.flush();
} catch (IOException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
new Main();
}
}
public void TestCommandRun(){
Process process = null;
String[] command_arr = new String[]{"cmd.exe","/K","start"};
ProcessBuilder pBuilder = new ProcessBuilder("C:/Windows/System32/cmd.exe");
try{
process = pBuilder.start();
}
catch(IOException e){
e.printStackTrace();
System.out.println("Process failed");
}
if(null != process){
OutputStream out = process.getOutputStream();
OutputStreamWriter outWriter = new OutputStreamWriter(out);
BufferedWriter bWriter = new BufferedWriter(outWriter);
try{
bWriter.write("dir");
bWriter.newLine();
bWriter.write("ipconfig");
bWriter.flush();
bWriter.close();
}
catch(IOException e){
e.printStackTrace();
System.out.println("bWriter Failed");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.