0

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?

Community
  • 1
  • 1
ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • Binary returns different code. Only way to figure it out is to debug. – Karol Dowbecki Jan 24 '19 at 15:08
  • @karoldowbecki So Apparently the command only passes the first word in my argument for some reason. [command] "i". I think i might need to format the string in a way – ninesalt Jan 24 '19 at 15:32

1 Answers1

0

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 });
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111