1

I am using ProcessBuilder to run a Linux command on a server:

ProcessBuilder pb = new ProcessBuilder("/usr/bin/printf %b", sendMessage,
                URL, " @serendipity | /usr/bin/perl /usr/local/bin/foo/bar -u nagios -s");

I am trying to broadcast a message that will be piped to a paging system called bar. But when executing the jar file on the server, I constantly get this:

java.io.IOException: Cannot run program "/usr/bin/printf %b": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at sms_serendipity.sms_serendipity.SmsSendMessage.sendMessage(SmsSendMessage.java:59)
    at sms_serendipity.sms_serendipity.SmsSendMessage.randomizeLinks(SmsSendMessage.java:48)
    at sms_serendipity.sms_serendipity.SmsParseWeb.regexHttp(SmsParseWeb.java:103)
    at sms_serendipity.sms_serendipity.SmsParseWeb.parseXML(SmsParseWeb.java:77)
    at sms_serendipity.sms_serendipity.SmsParseWeb.locateWebAudio(SmsParseWeb.java:44)
    at sms_serendipity.sms_serendipity.mainClass.main(mainClass.java:11)
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:247)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 6 more

It's my first time using ProcessBuilder (I have also tried with Runtime.exec() as well). Can someone tell me what I may do to correct the command I am trying to run?

ryekayo
  • 2,341
  • 3
  • 23
  • 51
  • Java's going to interpret those commands *literally* from left to right. Were you intending to get some string interpolation going there? – Makoto May 18 '17 at 17:33
  • Yes i was. Its basically broadcasting message that has some Strings – ryekayo May 18 '17 at 17:34
  • Possible duplicate of [Difference between ProcessBuilder and Runtime.exec()](http://stackoverflow.com/questions/6856028/difference-between-processbuilder-and-runtime-exec) – zero298 May 18 '17 at 18:25
  • @zero298 This is not a duplicate whatsoever. I am not asking for the differences between ProcessBuider and Runtime.exec() – ryekayo May 18 '17 at 18:27
  • You are misusing `new ProcessBuilder()` as though it were `Runtime.exec()`. You are, as @Henry pointed out, trying to pass a *command* to `ProcessBuilder()` instead of program name. That question details how to use `ProcessBuilder()` and explicitly calls out that difference. I've seen other questions closed as duplicates in this vein. I'm not saying this is a bad question, just that that other answer covers your problem. – zero298 May 18 '17 at 18:30
  • Ahh i see. I will take a look – ryekayo May 18 '17 at 18:36

2 Answers2

1

Read the error message carefully: you try to execute the program /usr/bin/printf %b which of course does not exist.

The program is called /usr/bin/printf.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • If i take out a %b it complains an operand is missing – ryekayo May 18 '17 at 18:22
  • `new ProcessBuilder("/usr/bin/printf", "%b", ...` – Henry May 18 '17 at 18:24
  • Tried that, thats much better. but it does not execute the perl script.. – ryekayo May 18 '17 at 18:26
  • Because the pipe is interpreted only by a shell. You will have to run the command with a shell. – Henry May 18 '17 at 18:27
  • Do you mean the whole command? Would you please be able to provide an example with the given command i provided? – ryekayo May 18 '17 at 18:28
  • Ok cool, one last thing: when I add dash -c to the beginning it complains about printf missing an operand, even though I have added that just like the command gave me.. Do you know why that may be? – ryekayo May 18 '17 at 18:33
0

I have figured out a way of getting this working. It took a bit of experimenting but here is what I did.

    ProcessBuilder pb = new ProcessBuilder(
            "/bin/dash", 
            "-c",
            "/usr/bin/perl /usr/local/bin/foo/bar -u nagios -s " + sendMessage + URL + fooUser,
            "/bin/echo");

I had it log the stdout to a text file and confirmed that the broadcast works.

ryekayo
  • 2,341
  • 3
  • 23
  • 51