This question already has answers here:
Runtime's exec() method is not redirecting the output
                                
                                    (2个答案)
                                
                        
                去年关闭。
            
        

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();
        }
    }
}


这是我的项目结构:

java - 我想从Java程序运行pict.exe-LMLPHP

最佳答案

您需要使用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();

10-08 08:48