这可能很容易 - 但是此时它使我发疯。我正在尝试从Mac计算机上的处理运行顺利运行,没有问题。我需要将代码迁移到Windows 7机器,但由于某种原因无法使其工作。通过处理与终端交谈效果很好。我在正确的文件夹(草图数据文件夹也在Sox也被插入),因为我可以运行诸如" dir"等的命令,并打印正确的内容 - 但是一旦我尝试运行Sox.exe,任何事情都不会发生(获取(获取)出口值1)。直接从CMD终端运行Sox.exe工作正常。这是我想做的事情的示例:
void playBackYear (){
soxPlay = "cmd /c sox.exe year.wav -d";
println (soxPlay);
try {
File workingDir = new File(sketchPath("data"));
Process p=Runtime.getRuntime().exec(soxPlay, null, workingDir);
p.waitFor();
BufferedReader reader=new BufferedReader(
new InputStreamReader(p.getInputStream())
);
String line;
while ( (line = reader.readLine ()) != null)
{
println(line);
}
int exitVal = p.waitFor();
System.out.println("Exited with error code "+exitVal);
}
catch(IOException e1) {
System.err.println("Caught IOException: " + e1.getMessage());
System.out.println( "error 1" );
}
catch(InterruptedException e2) {
System.err.println("Caught IOException: " + e2.getMessage());
System.out.println( "error 2" );
}
}
所以问题是我在这里做错了什么?任何帮助都将不胜感激。
我编写了一个小型包装器应用程序,该应用程序包裹在Java中。如果您对整个项目感兴趣,请在GitHub上查看:Sox Java包装项目
这是我解决问题的方法:
private List<String> arguments = new ArrayList<String>();
// add sox arguments to this list above
public void execute() throws IOException {
File soxBinary = new File(soXBinaryPath);
if (!soxBinary.exists()) {
throw new FileNotFoundException("Sox binary is not available under the following path: " + soXBinaryPath);
}
arguments.add(0, soXBinaryPath);
logger.debug("Sox arguments: {}", arguments);
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
processBuilder.redirectErrorStream(true);
Process process = null;
IOException errorDuringExecution = null;
try {
process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
logger.debug(line);
}
} catch (IOException e) {
errorDuringExecution = e;
logger.error("Error while running Sox. {}", e.getMessage());
} finally {
arguments.clear();
if (process != null) {
process.destroy();
}
if (errorDuringExecution != null) {
throw errorDuringExecution;
}
}
}