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?

8
  • 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. Commented Apr 5, 2015 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. Commented Apr 5, 2015 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 :). Commented Apr 5, 2015 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. Commented Apr 5, 2015 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... Commented Apr 5, 2015 at 15:14

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.