在其参数之间执行带有空格的Runtime.getRuntime()。exec()

时间:2016-08-19 09:51:09

标签: java runtime executable

所有专家的好日子,

希望对我这个微不足道的问题有一些建议。

我尝试在将参数testcase1testdata1testresult传递给jar_filepathtctest_data时执行以下语法和test_result分别

Runtime.getRuntime().exec("cmd /c start cmd /k java -jar "+jar_filepath + tc + test_data + test_result);

然而,执行时,出现如下错误: Error: Unable to access jarfile C:\TestAutomation\Jar\Test1.jartestcase1testdata1testresult2其中所有论点都是'空间不适用。

希望有关于在exec()级别编写执行代码的正确方法的建议。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

你的方式在参数之前添加空格。这看起来像这样:

Runtime.getRuntime().exec("cmd /c start cmd /k java -jar "
    + jar_filepath + " " + tc + " " + test_data + " " + test_result);

但如果您使用String[],则会自动执行此操作:

String[] command = {
    "cmd /c start",
    "cmd /k java -jar " + jar_filepath,
    tc,
    test_data,
    test_result
};

Runtime.getRuntime().exec(comand);