简体   繁体   English

如何通过Java运行多个cmd命令?

[英]How to run multiple cmd commands through Java?

I have a simple GUI which selects an executable and a batch file.我有一个简单的 GUI,它选择一个可执行文件和一个批处理文件。 Clicking "run" should launch a command line instance, then run the executable given the selected batch.单击“运行”应该会启动一个命令行实例,然后根据选定的批次运行可执行文件。 However, hiccups seem to show up at different points.然而,打嗝似乎出现在不同的点上。 This is the relevant code snippet:这是相关的代码片段:

String[] commands = {"cmd.exe", "/c", "C:\\Xilinx\\14.7\\ISE_DS\\settings64.bat && cd /d ",
                "\"" + simFile.getParent() + "\"", " && ping localhost && ",
                "\"" + jTextField1.getText() + "\"",  " -tclbatch \"" + jTextField2.getText() + "\""};

ProcessBuilder simBuilder = new ProcessBuilder(commands);
simBuilder.redirectErrorStream(true);
Process simulation = simBuilder.start();

BufferedReader reader = new BufferedReader(new InputStreamReader(simulation.getInputStream()));
String line;
while (true) {
    line = reader.readLine();
    if (line == null)
        break;
    System.out.println(line);
}

I chose to create a process through a ProcessBuilder rather than "Runtime.getRuntime().exec" because having the command and arguments as a String array is more readable and manageable.我选择通过 ProcessBuilder 而不是“Runtime.getRuntime().exec”来创建进程,因为将命令和 arguments 作为字符串数组更具可读性和可管理性。 I took a look through the documentation of Runtime, Process, and ProcessBuilder.我查看了 Runtime、Process 和 ProcessBuilder 的文档。 I also searched for similar questions, the following being the closest: Run cmd commands through Java .我还搜索了类似的问题,以下是最接近的问题: 通过 Java 运行 cmd 命令 However, I'm still having issues getting all commands to run properly, if it all.但是,如果所有命令都正常运行,我仍然遇到问题。 First point: The program successfully executes the commands until "ping", which I placed to determine where the issue occurs.第一点:程序成功执行命令直到“ping”,我将其放置以确定问题发生的位置。 I get the cmd output in the console through the BufferedReader just fine.我通过 BufferedReader 在控制台中得到 cmd output 就好了。 However, the next command, which should run the executable indicated by "jTextField1.getText()", gives an error of "The filename, directory name, or volume label syntax is incorrect" although I made sure the path is within escaped double quotes to account for spaces.然而,下一个命令应该运行由“jTextField1.getText()”指示的可执行文件,给出错误“文件名、目录名或卷 label 语法不正确”,尽管我确保路径在转义双引号内占空间。 Is it something in my syntax?这是我的语法吗? Something to do with where the double ampersands are placed?与双符号的放置位置有关吗? Does every separate command with its argument need to be its own string in the array?是否每个带有参数的单独命令都需要在数组中是它自己的字符串? I tried that and different things, but it always seems to result in an error.我尝试了那个和不同的东西,但它似乎总是导致错误。

You should check that your path names are correct, and try the cmd as one parameter value not as comma separated after cmd.exe /c .您应该检查您的路径名是否正确,并尝试将 cmd 作为一个参数值,而不是在cmd.exe /c之后以逗号分隔。 This will ensure the arguments are passed to CMD correctly as a single argument for the CMD shell to handle:这将确保 arguments 作为 CMD shell 的单个参数正确传递给 CMD 以处理:

import java.nio.file.Files
System.out.println("Files.isDirectory(simFile.getParent())="+Files.isDirectory(simFile.getParent()));
System.out.println("Files.isExecutable(jTextField1.getText())="+Files.isExecutable(Path.of(jTextField1.getText())));

String cmd = "C:\\Xilinx\\14.7\\ISE_DS\\settings64.bat && cd /d "+
            "\"" + simFile.getParent() + "\" && ping localhost && "+
            "\"" + jTextField1.getText() + "\" -tclbatch \"" + jTextField2.getText() + "\"";
String[] commands = {"cmd.exe", "/c", cmd};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM