Java runtime binary shows different results

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"";

Process p = rt.exec(command);
Scanner s = new Scanner(p.getInputStream()).useDelimiter("\A");
String output = s.hasNext() ? : "";

This prints:

/home/ninesalt/repositories/elasticsearch-ingest-opennlp/src/main/go/Sentiment “i love this”


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

Leave a Reply

Your email address will not be published. Required fields are marked *