ProcessBuilder 不输出任何内容
ProcessBuilder Does Not Output Anything
我 运行 使用 Process Builder 的 shell 命令:
try {
ProcessBuilder lmBuilder = new ProcessBuilder("/home/istanbul/srilm/bin/i686-m64/ngram-count", "-text", modelPath, "-order", "3", "-lm", modelOutputPath);
lmBuilder.redirectErrorStream();
final Process lmProcess = lmBuilder.start();
int result = lmProcess.waitFor(); //result becomes 0
InputStream is = lmProcess.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
当我检查命令的输出时,我发现它工作正常。当我通过 shell 运行 它时,我看到输出:
warning: discount coeff 1 is out of range: 0
warning: discount coeff 7 is out of range: 1.06876
warning: discount coeff 7 is out of range: 2.09945
然而,当我 运行 它 通过进程构建器时,我看不到任何输出 。
有什么想法吗?
lmBuilder.redirectErrorStream();
这是不正确的。
此方法仅告诉您是否将 stderr 重定向到 stdout;它没有指示应该将 stderr 重定向到它。
你应该使用的是:
lmBuilder.redirectErrorStream(true);
我 运行 使用 Process Builder 的 shell 命令:
try {
ProcessBuilder lmBuilder = new ProcessBuilder("/home/istanbul/srilm/bin/i686-m64/ngram-count", "-text", modelPath, "-order", "3", "-lm", modelOutputPath);
lmBuilder.redirectErrorStream();
final Process lmProcess = lmBuilder.start();
int result = lmProcess.waitFor(); //result becomes 0
InputStream is = lmProcess.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
当我检查命令的输出时,我发现它工作正常。当我通过 shell 运行 它时,我看到输出:
warning: discount coeff 1 is out of range: 0
warning: discount coeff 7 is out of range: 1.06876
warning: discount coeff 7 is out of range: 2.09945
然而,当我 运行 它 通过进程构建器时,我看不到任何输出 。
有什么想法吗?
lmBuilder.redirectErrorStream();
这是不正确的。
此方法仅告诉您是否将 stderr 重定向到 stdout;它没有指示应该将 stderr 重定向到它。
你应该使用的是:
lmBuilder.redirectErrorStream(true);