I'm trying to run a binary using my Java code but it's giving me a different result than when I would run it myself from my terminal.
Java code:
Runtime rt = Runtime.getRuntime();
String path = System.getProperty("user.dir") + "/src/main/go/Sentiment";
String command = path + " " + "\"i love this\"";
System.out.println(command);
Process p = rt.exec(command);
Scanner s = new Scanner(p.getInputStream()).useDelimiter("\\A");
String output = s.hasNext() ? s.next() : "";
System.out.println(output);
This prints:
/home/ninesalt/repositories/elasticsearch-ingest-opennlp/src/main/go/Sentiment "i love this"
0
However when I run in that same exactly command in my terminal I get 1 instead. Why is this happening?
Switch to Runtime.exec(String[])
to avoid problems in quoting the arguments:
String command = System.getProperty("user.dir") + "/src/main/go/Sentiment";
String arg = "i love this";
Process p = rt.exec(new String[] { command, arg });