【发布时间】:2013-12-27 20:56:20
【问题描述】:
我正在使用 mkvmerge 将 avi 文件和 ass 文件合并为 mkv 文件。
我拥有可执行文件和 java 文件的当前目录位于名为 auto-mkvmerge 的文件夹中。
当我在命令行中使用以下命令时,它可以正常工作,mkvmerge.exe -o ../auto-done/098.mkv ../auto-vid/098.avi ../auto-sub/098.ass 我得到了正确的输出。
当我运行我的 java 代码时,它不起作用,并给我一个错误。
我的 java 代码。 主.java
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
File vidDir = new File(args[0]);
File subDir = new File(args[1]);
File outDir = new File(args[2]);
String[] vids = vidDir.list();
String[] subs = subDir.list();
for(int i = 0; i < vids.length; i++) {
int r = mergeAviAndAss(vids[i], subs[i], args[2]);
}
}
private static int mergeAviAndAss(String aviFileName, String assFileName, String doneDir) {
try {
ProcessBuilder pb = new ProcessBuilder("mkvmerge.exe", "-o", doneDir + "/" + aviFileName.substring(0, aviFileName.length() - 4) + ".mkv", "\"" + aviFileName + "\"", "\"" + assFileName + "\"");
Process p = pb.start();
InputStream inputStream = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = br.readLine()) != null)
System.out.println(line);
return p.waitFor();
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}
return -1;
}
}
当我使用以下行执行它时(编译我的 java 代码之后):java Main ../auto-vid ../auto-sub ../auto-done > log.txt
mkvmerge v6.6.0 ('The Edge Of The In Between') built on Dec 1 2013 17:55:00
Error: The file '098.avi' could not be opened for reading: open file error.
mkvmerge v6.6.0 ('The Edge Of The In Between') built on Dec 1 2013 17:55:00
Error: The file '099.avi' could not be opened for reading: open file error.
mkvmerge v6.6.0 ('The Edge Of The In Between') built on Dec 1 2013 17:55:00
Error: The file '100.avi' could not be opened for reading: open file error.
mkvmerge v6.6.0 ('The Edge Of The In Between') built on Dec 1 2013 17:55:00
Error: The file '101.avi' could not be opened for reading: open file error.
mkvmerge v6.6.0 ('The Edge Of The In Between') built on Dec 1 2013 17:55:00
Error: The file '102.avi' could not be opened for reading: open file error.
mkvmerge v6.6.0 ('The Edge Of The In Between') built on Dec 1 2013 17:55:00
Error: The file '103.avi' could not be opened for reading: open file error.
我的代码是否有问题,或者我编写它的方式有问题,因为如果我在命令行中键入它时它可以正常工作,它应该可以在我的 java 代码中工作。
【问题讨论】:
-
打开文件错误是否意味着文件无法打开,因为它没有找到,或者因为它当前有一些排他锁?可能很笨,但你确定你没有在另一个应用程序中打开文件吗?
-
是的,我检查了,我什至打印了文件列表以检查它们的名称和扩展名是否正确,并且我确保它们没有打开。
-
您可以尝试将构建的字符串写入您传递给流程构建器的 STDout,以验证该字符串没有任何问题吗?
-
工作目录也可能对您的可执行文件很重要。尝试使用 pb.directory("PATH WITH THE FILES IN IT HERE"); 在您的流程构建器上设置工作目录在开始这个过程之前。
-
我尝试将avi和ass文件与mkvmerge.exe和我的java文件放在同一目录下,它工作正常
标签: java command-line processbuilder