我想从Java程序运行pict.exe

时间:2019-03-06 10:54:54

标签: java process runtime executable

PICT生成测试用例和测试配置。 https://github.com/microsoft/pict

我想通过Java程序运行可执行文件pict.exe。我尝试使用Runtime.getRuntime.exec(),它运行时没有任何错误,但是没有生成输出文件。

package com.test.CommandLine;

public class CommandLineTest {

    public static void main(String[] args) {
        String execPath = "resources\\pict.exe";
        String param = "resources\\CarOrderApp.txt > resources\\CarOrderApp.xls";

        runPict(execPath, param);
    }

    public static void runPict(String execPath, String param) {
        try {
            Process process = Runtime.getRuntime().exec(execPath + " " + param);
        } 
        catch (Exception e) {
            System.out.println(e.getMessage() + "\n");
            e.printStackTrace();
        }
    }
}

这是我的项目结构:

enter image description here

1 个答案:

答案 0 :(得分:2)

您需要使用ProcessBuilder定义输出,而不是使用cmd >重定向运算符:

String execPath = "resources\\pict.exe";
String inPath = "resources\\CarOrderApp.txt";
String outPath = "resources\\CarOrderApp.xls";

ProcessBuilder builder = new ProcessBuilder(execPath, inPath);
builder.redirectOutput(new File(outPath));
Process p = builder.start();
p.waitFor();