本文介绍了ProcessBuilder 给出“没有这样的文件或目录"在 Mac 上,而 Runtime().exec() 工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Playframework 上运行的应用程序,它需要对一些视频文件进行编码.我用过

Process pr = Runtime.getRuntime().exec(execCode)

为此(并且它完美地工作),但由于我需要输出流和错误流,我正在尝试使用 ProcessBuilder(也推荐使用).

但我无法让它工作(在 MacBook 上测试).Runtime 方法和 ProcessBuilder 之间有根本区别吗?

这是我的 ProcessBuilder 代码(用 Runtime.getRuntime().exec() 替换时,代码完全相同)

 String execCode = "/opt/local/bin/ffmpeg -i file [...]";ProcessBuilder pb = new ProcessBuilder(execCode);pb.redirectErrorStream(true);pb.directory(new File("/Users/[...]/data/"));进程 pr = pb.start();

这是控制台输出:

11:00:18,277 错误 ~ 处理 MediaFile[13] 时出现问题,错误编码过程中出错:无法运行程序/opt/local/bin/ffmpeg -i/Users/[...]/data/media/1/1/test.mov [...]/Users/[...]/data/media/1/13/encoded.mp3"(在目录/Users/[...]/data"): error=2, 没有那个文件或目录java.lang.Exception:编码过程中出错:无法运行程序/opt/local/bin/ffmpeg -i/Users/Luuk/Documents/Java/idoms-server/data/media/1/1/test.mov -y -f mpegts -acodec libmp3lame -ar 48000 -b:a 64000 -vn -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_int25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate -1 -bufsize -1 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -g 30 -a/Users/Luuk/Documents/Java/idoms-server/data/media/1/13/encoded.mp3"(在目录/Users/Luuk/Documents/Java/idoms-server/data"中):错误=2,无此文件或目录在 logic.server.MediaCoder.encodeMediaFile(MediaCoder.java:313)在 logic.server.MediaCoder.doJob(MediaCoder.java:54)在 play.jobs.Job.doJobWithResult(Job.java:50)在 play.jobs.Job.call(Job.java:146)在 play.jobs.Job$1.call(Job.java:66)在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)在 java.util.concurrent.FutureTask.run(FutureTask.java:138)在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)在 java.lang.Thread.run(Thread.java:680)引起:java.io.IOException:无法运行程序/opt/local/bin/ffmpeg -i/Users/Luuk/Documents/Java/idoms-server/data/media/1/1/test.mov -y -f mpegts -acodec libmp3lame -ar 48000 -b:a 64000 -vn -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate -1 -bufsize -1 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -g 30 -async 2/Users/Luuk/Documents/Java/idoms-server/data/media/1/13/encoded.mp3"(在目录/Users/Luuk/Documents/Java/idoms-server/data"中):错误=2,没有这样的文件或目录在 java.lang.ProcessBuilder.start(ProcessBuilder.java:460)在 logic.server.MediaCoder.encodeMediaFile(MediaCoder.java:189)……还有 11 个引起:java.io.IOException: error=2, No such file or directory在 java.lang.UNIXProcess.forkAndExec(本机方法)在 java.lang.UNIXProcess.(UNIXProcess.java:53)在 java.lang.ProcessImpl.start(ProcessImpl.java:91)在 java.lang.ProcessBuilder.start(ProcessBuilder.java:453)……还有 12 个
解决方案

您需要将参数指定为单独的Strings:

new ProcessBuilder("cmd", "arg1", "arg2", ...);

构造函数接受 StringvarargsList.

请参阅 ProcessBuilder 文档.>

I have an application, running on the Playframework, which needs to encode some video files. I used

Process pr = Runtime.getRuntime().exec(execCode)

for this (and it works perfectly), but as I need both, the output stream and the error stream, I am trying to use ProcessBuilder (as is also recommended).

But I cannot get it to work (testing on a MacBook). Is there a fundamental difference between the Runtime method and the ProcessBuilder?

This is my code for ProcessBuilder (exactly the same code works when replaced by Runtime.getRuntime().exec())

    String execCode = "/opt/local/bin/ffmpeg -i file [...]";
    ProcessBuilder pb = new ProcessBuilder(execCode);
    pb.redirectErrorStream(true);
    pb.directory(new File("/Users/[...]/data/"));
    Process pr = pb.start();

This is the console output:

11:00:18,277 ERROR ~ There was a problem with with processing MediaFile[13] with error Error during coding process: Cannot run program "/opt/local/bin/ffmpeg -i /Users/[...]/data/media/1/1/test.mov [...] /Users/[...]/data/media/1/13/encoded.mp3" (in directory "/Users/[...]/data"): error=2, No such file or directory
java.lang.Exception: Error during coding process: Cannot run program "/opt/local/bin/ffmpeg -i /Users/Luuk/Documents/Java/idoms-server/data/media/1/1/test.mov -y -f mpegts -acodec libmp3lame -ar 48000 -b:a 64000 -vn -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate -1 -bufsize -1 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30  -g 30 -async 2 /Users/Luuk/Documents/Java/idoms-server/data/media/1/13/encoded.mp3" (in directory "/Users/Luuk/Documents/Java/idoms-server/data"): error=2, No such file or directory
    at logic.server.MediaCoder.encodeMediaFile(MediaCoder.java:313)
    at logic.server.MediaCoder.doJob(MediaCoder.java:54)
    at play.jobs.Job.doJobWithResult(Job.java:50)
    at play.jobs.Job.call(Job.java:146)
    at play.jobs.Job$1.call(Job.java:66)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.io.IOException: Cannot run program "/opt/local/bin/ffmpeg -i /Users/Luuk/Documents/Java/idoms-server/data/media/1/1/test.mov -y -f mpegts -acodec libmp3lame -ar 48000 -b:a 64000 -vn -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate -1 -bufsize -1 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30  -g 30 -async 2 /Users/Luuk/Documents/Java/idoms-server/data/media/1/13/encoded.mp3" (in directory "/Users/Luuk/Documents/Java/idoms-server/data"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at logic.server.MediaCoder.encodeMediaFile(MediaCoder.java:189)
    ... 11 more
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
    at java.lang.ProcessImpl.start(ProcessImpl.java:91)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 12 more
解决方案

You need to specify the arguments as separate Strings:

new ProcessBuilder("cmd", "arg1", "arg2", ...);

The constructor accepts String, varargs, and List<String>.

See ProcessBuilder documentation.

这篇关于ProcessBuilder 给出“没有这样的文件或目录"在 Mac 上,而 Runtime().exec() 工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 14:09