javaterminalechoprocessbuildermoses

How to write this perticular command without echo concatenation?


I am using this command as given in the tutorial here http://www.statmt.org/moses/?n=Moses.Baseline

echo 'T W O N E I G H T' | /home/saj/g2p/mosesdecoder-master/bin/moses -f /home/saj/g2p/working/binarised-model/moses.ini

It is working fine and correctly but I need to run it without echo command. Because I wanted to run this command in JAVA (Eclipse) and there is problem with concatenation. Even

      Process p = r.exec("echo '/home/saj/' | ls");

is also not running. Though simple commmands like ls,pwd are working correctly.

I tried these things but none of them is working..

/home/saj/g2p/mosesdecoder-master/bin/moses -f /home/saj/g2p/working/binarised-model/moses.ini 'T W O N E I G H T'

/home/saj/g2p/mosesdecoder-master/bin/moses -f /home/saj/g2p/working/binarised-model/moses.ini T W O N E I G H T

/home/saj/g2p/mosesdecoder-master/bin/moses 'T W O N E I G H T' -f /home/saj/g2p/working/binarised-model/moses.ini

/home/saj/g2p/mosesdecoder-master/bin/moses T W O N E I G H T -f /home/saj/g2p/working/binarised-model/moses.ini

Please suggest correct command to run without echo.


Solution

  • Since your arguments contain spaces, you cannot rely on the built-in tokenization. To avoid this, use exec(String[]) rather than exec(String). For example, for this command:

    /home/saj/g2p/mosesdecoder-master/bin/moses -f \
        /home/saj/g2p/working/binarised-model/moses.ini 'T W O N E I G H T'
    

    You would do this:

    String args[] = new String[] {
        "/home/saj/g2p/mosesdecoder-master/bin/moses",
        "-f",
        "/home/saj/g2p/working/binarised-model/moses.ini",
        "T W O N E I G H T" };
    Process p = r.exec(args);
    

    Also, regarding piping and redirection, note that these are handled by the shell. In order to run a command line such as echo '/home/saj/' | ls from Java, you should execute a shell and pass this to the shell as an argument; for example:

    String args[] = new String[] { "/bin/sh", "-c", "echo '/home/saj/' | ls" };
    Process p = r.exec(args);