1

我想执行一个程序(System.getenv("appdata") + "ffmpeg")。我还希望能够获得一个流程或可以让我获得控制台输出的东西。我之前尝试过“cmd /C”+ System.getenv("appdata") + "ffmpeg",但似乎没有用。任何帮助表示赞赏!

这是一些代码:

Process p = exec(testFFMpeg);
    int ex = -1;
    try {
        ex = p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
        CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions");
    }

    if(ex == 0){
        System.out.println("Normal execution, exit value: " + ex);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;

        do{
            try {
                line = br.readLine();
                System.out.println(line);
            } catch (IOException e) {
                e.printStackTrace();
                CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions");
            }
        }while(line != null);
    }else{
        System.out.println("Execution exit value: " + ex);
    }
}

private static Process exec(String[] cmd){
    try {
        return Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        e.printStackTrace();
        CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions");
    }

该文件的确切位置是:`System.getenv("appdata") + "\VinVid\" + "ffmpeg.exe"。

4

3 回答 3

1

我想到了。输出到错误流,而不是输入流。

于 2013-10-06T04:21:16.493 回答
0

您缺少路径分隔符,试试这个

System.getenv("appdata") + "\\ffmpeg"
于 2013-10-06T03:29:47.773 回答
0

在您的代码中,将此行更改line = br.readLine();line += br.readLine(); This is how you run a windows process in java。我不知道你想要什么输出。如果您要求打印您运行的进程的输出,此代码会有所帮助:

String line;
String pidInfo ="";

Process p =Runtime.getRuntime().exec(System.getenv("appdata") +"ffmpeg.exe");

BufferedReader input =  new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {
    pidInfo+=line; 
}

input.close();

System.out.println(pidInfo);
于 2013-10-06T03:35:57.320 回答