3

I want to run a shell command from my Java application. I have compiled a software called "SRILM" and when I run it from shell everything is OK:

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

However when I run it from my Java application:

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();
}

I get that error:

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

Any ideas?

kamaci
  • 72,915
  • 69
  • 228
  • 366
  • So, the full path to your executable is `/home/istanbul/srilm/bin/i686-m64/ngram-count/ngram-count`? I assume you should use `lmBuilder.directory(new File("/home/istanbul/srilm/bin/i686-m64/"));` instead. – Tom Apr 05 '15 at 14:38
  • @Tom I've changed it, now I get error=2. Actually that program is under path and I can run it (ngram-count) from any directory at shell. – kamaci Apr 05 '15 at 15:02
  • Well, you said the processbuilder, that you provide a working directory, so it expects one, but `/home/istanbul/srilm/bin/i686-m64/ngram-count` is a file, hence the first exception. Can you update your question with the new exception? Oh, already done, ok :). – Tom Apr 05 '15 at 15:08
  • Are you still using the same code? Btw `No such file or directory` is pretty clear. Check that the provided directory/file really exists. – Tom Apr 05 '15 at 15:12
  • Yes, still same code. Also I've tried this one: `ProcessBuilder lmBuilder = new ProcessBuilder("/bin/sh" ,"ngram-count -text " + modelPath + "-order 3 -lm /home/furkan/Desktop/snlp_hmm/hohom.lm");` but it does not work... – kamaci Apr 05 '15 at 15:14
  • Btw, when I run it from shell it works.So, it is not a problem about Path of executable. – kamaci Apr 05 '15 at 15:15
  • So, you're still using `lmBuilder.directory(new File("/home/istanbul/srilm/bin/i686-m64/ngram-count"));`? Is this really a directory? And if you still use the same code, how can you get different results? – Tom Apr 05 '15 at 15:15
  • Please check this question, it might help: http://stackoverflow.com/questions/9847242/processbuilder-cant-find-file. It seems, that `.directory(...)` doesn't tell `ProcessBuilder` where to look for the executable. – Tom Apr 05 '15 at 15:24

1 Answers1

3

When I run it like that it worked:

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