【发布时间】:2021-12-22 10:44:00
【问题描述】:
我对使用 Junit 5 运行脚本有疑问。我有以下代码:
public class RunMvnSubprocess {
@Test
public void main() throws IOException, InterruptedException {
String[] cmd = new String[]{"mvn.cmd", "-version"}; // command to be executed on command prompt.
Process p = Runtime.getRuntime().exec(cmd);
try (BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = output.readLine()) != null) {
System.out.println(line);
}
}
p.waitFor();
}
}
当我使用 Junit 5.7.0 运行它时,没有输出。但是,在 Junit 4.13.2 上运行它可以正常工作。
请注意,我在 Windows 10 Pro 版本 21H1 中运行此测试。
编辑:
修改
new String[]{"mvn.cmd", "-version"}
到
new String[]{"cmd", "/c", "\"mvn -version\""}
对我有用,但启动子外壳是一种不好的做法,因此我将这种解决方法作为最后的手段。
【问题讨论】: