Process Builder java.io.IOException错误= 2

时间:2015-04-05 14:33:10

标签: java processbuilder

我想从我的Java应用程序运行一个shell命令。我已经编译了一个名为“SRILM”的软件,当我从shell运行它时,一切正常:

ngram-count -text /home/istanbul/Desktop/snlp_hmm/model.txt -order 3 -lm hoho.lm

但是,当我从Java应用程序运行它时:

ProcessBuilder lmBuilder = new ProcessBuilder("ngram-count", "-text", modelPath, "-order", "3", "-lm", "hohom.lm");
lmBuilder.directory(new File("/home/istanbul/srilm/bin/i686-m64"));

try {
    final Process lmProcess = lmBuilder.start();
    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 e) {
    e.printStackTrace();
}

我收到了这个错误:

java.io.IOException: Cannot run program "ngram-count" (in directory "/home/istanbul/srilm/bin/i686-m64"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
    at org.itu.hmm.AlgorithmRunner.evaluate(AlgorithmRunner.java:127)
    at org.itu.hmm.ApplicationRunner.main(ApplicationRunner.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
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:186)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
    ... 7 more

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

当我像它那样运行它时:

ProcessBuilder lmBuilder = new ProcessBuilder("/home/istanbul/srilm/bin/i686-m64/ngram-count", "-text", modelPath, "-order", "3", "-lm", "/home/istanbul/Desktop/snlp_hmm/j.lm");