有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java apache commons exec,在同一“上下文”中运行2个命令

我不确定上下文是否适合在这里使用,但我的意思是

cd .\test
test.exe

测试。exe位于文件夹测试中,我想从文件夹测试中运行它,我知道我可以运行

.\test\test.exe

但我需要测试。要从文件夹测试运行的exe

有没有办法在同一个“上下文”中运行这两个命令

我试过:

String cmd1 = "cmd /C cd test";
String cmd2 = "test.exe";
CommandLine cmdl1 = CommandLine.parse(cmd1);
CommandLine cmdl2 = CommandLine.parse(cmd2);
DefaultExecutor exec = new DefaultExecutor();
exec.execute(cmdl1);
exec.execute(cmdl2);

但正如所料,它找不到test.exe


共 (1) 个答案

  1. # 1 楼答案

    我会尝试使用&&操作符执行结合在一起的两个命令

    比如:

    cd .\test && .\test.exe
    

    这将首先更改目录,如果成功,则在该目录中执行test.exe可执行文件。如果更改目录不成功,命令的后半部分将不会执行