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?

6
  • Java's going to interpret those commands literally from left to right. Were you intending to get some string interpolation going there? Commented May 18, 2017 at 17:33
  • Yes i was. Its basically broadcasting message that has some Strings Commented May 18, 2017 at 17:34
  • Possible duplicate of Difference between ProcessBuilder and Runtime.exec() Commented May 18, 2017 at 18:25
  • @zero298 This is not a duplicate whatsoever. I am not asking for the differences between ProcessBuider and Runtime.exec() Commented May 18, 2017 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. Commented May 18, 2017 at 18:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

If i take out a %b it complains an operand is missing
new ProcessBuilder("/usr/bin/printf", "%b", ...
Tried that, thats much better. but it does not execute the perl script..
Because the pipe is interpreted only by a shell. You will have to run the command with a shell.
Do you mean the whole command? Would you please be able to provide an example with the given command i provided?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.