ProcessBuilder fails on command that includes local environment variable - java

I can execute usual commands on Linux, wrapped by the processBuilder. But I'm currently trying to run the minecraft server like in the following example, with some variable set before the command, and it fails with an exception.
final ProcessBuilder processBuilder = new ProcessBuilder("LD_LIBRARY_PATH=. ./bedrock_server");
processBuilder.directory(MC_PAL_LOCATION_DIR.toFile());
process = processBuilder.start();
Exception:
java.io.IOException: Cannot run program "LD_LIBRARY_PATH=. ./bedrock_server" (in directory "/home/user/Desktop/minecraft_bedrock_server_t"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at controller.Server.startMinecraftServer(Server.java:91)
at controller.Server.start(Server.java:58)
at Bootstrapper.bootServer(Bootstrapper.java:67)
at Bootstrapper.main(Bootstrapper.java:30)
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)
... 4 more
Exception in thread "Thread-0" java.lang.NullPointerException
at controller.ConsoleInput.run(ConsoleInput.java:16)
at java.lang.Thread.run(Thread.java:748)
Is there any possibility to use the processBuilder for such commands? The command works if I paste it directly to the terminal.
Link to server: https://minecraft.net/en-us/download/server/bedrock/
Command: LD_LIBRARY_PATH=. ./bedrock_server

You can't use bash shell commands like that without bash. But you can manipulate the environment yourself programmatically. Like,
final ProcessBuilder processBuilder = new ProcessBuilder("./bedrock_server");
processBuilder.environment().put("LD_LIBRARY_PATH", ".");
processBuilder.directory(MC_PAL_LOCATION_DIR.toFile());
process = processBuilder.start();

As #ElliottFrisch pointed out, you cannot use shell command without bash, therefore you either add LD_LIBRARY_PATH to environment map or execute bash:
final ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "LD_LIBRARY_PATH=. ./bedrock_server");
processBuilder.directory(MC_PAL_LOCATION_DIR.toFile());
process = processBuilder.start();

Related

ProcessBuilder("command","-v","date").start() fails with IOException: No such file or directory

I am trying to find out whether a command exists (eg. date) using the command shell builtin, on Ubuntu. However the following (scroll further below for java snippet)
//main.kt
fun main(){
val proc=ProcessBuilder("command","-v","date").start() //line 37
}
fails to run with stack trace
Exception in thread "main" java.io.IOException: Cannot run program "command": error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at MainKt.main(main.kt:37)
at MainKt.main(main.kt)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:314)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
... 3 more
command is definitely available on my bash -
user#pc:~$ type command
command is a shell builtin
Why does the error occur?
Note that most other similar queries (there are plenty on SO) got solved via syntactic corrections or bad file paths, and don't apply here.
Env:
JDK 17 on Ubuntu 20.04.5 LTS
Java code:
import java.io.IOException;
//rough.java
public class rough {
public static void main(String[] args) throws IOException {
new ProcessBuilder("command","-v","date").start();
}
}
Your first sentence already has the solution:
[…] using the command shell builtin […]
ProcessBuilder execs processes directly and does not invoke a shell. And since a shell-builtin is a functionality provided by the shell and not a binary, it cannot be invoked (directly) with ProcessBuilder.
If you want to run a shell, you need to do so explicitly:
new ProcessBuilder("sh", "-c", "command -v date").start();

Java: No such file or directory when redirecting command line program output to /dev/null

I'm currently working on invoking bash program using java. The bash program output too much message and I want to redirect them to /dev/null. But I encountered a weird error No such file or directory.
Here is my demo.
public static void main(String[] args) {
try {
// Another version I've tried:
// Process p = Runtime.getRuntime().exec("echo a > /dev/null");
ProcessBuilder b = new ProcessBuilder("echo a");
// b.redirectOutput(new File("/dev/null")).redirectErrorStream(true);
b.redirectOutput(ProcessBuilder.Redirect.to(new File("/dev/null")))
.redirectErrorStream(true);
Process p = b.start();
p.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
And the error message as follows:
java.io.IOException: Cannot run program "echo a": error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at test.main(test.java:12)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
... 2 more
I'm using a MacBook with Catalina, and I tried java 1.8.0_231 and 1.8.0_241 from oracle. (I couldn't use higher java version because one of the dependency of my project requires java 8).
To ignore the output from the process, it's easier and more portable to use ProcessBuilder.Redirect.DISCARD than explicitly redirecting to a special file/device such as /dev/null.
b.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectErrorStream(true);
Forget about using Runtime.exec - that method is badly designed and hard to use safely. If you want to do input redirection with the "> /dev/null" style, you need to remember that > is a construct created by the command interpreter shell, not the operating system, and if you want to use it you must run a shell.
Runtime.getRuntime().exec(new String[] {"sh", "-c", "echo a > /dev/null"});

Unable to run linux commands using Java

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?
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.
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.

ProcessBuilder can't find perl

I'm trying to execute a perl script from java with the following code:
ProcessBuilder script =
new ProcessBuilder("/opt/alert-ssdb.pl");
Process tmp = script.start();
But when I execute it it returns
java.io.IOException: Cannot run program "/opt/alert-ssdb.pl": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:488)
at scripttest.main(scripttest.java:11)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)
at java.lang.ProcessImpl.start(ProcessImpl.java:81)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
... 1 more
about the file
ls -l alert-ssdb.pl
-rwxr-xr-x. 1 root root alert-ssdb.pl
I tried running /usr/bin/perl/ with the script as an argument and it also failed with the same exception.
/bin/ls and other simple commands run without a problem though.
Also the first line of the script is #!/usr/bin/perl
and when run on command line it works
what am I missing?
//Update:
The big picture is that I'm trying to call the script via a storm bolt and it fails at that point.
I managed to make it work by defining a python script as a bolt
using
super(python,myscript.py)
(myscript imports the storm library) and from myscript I call the perl script.
I haven't tried yet but I suppose that If I modify the perl script to be a storm bolt it will run nicely.
Try changing
new ProcessBuilder("/opt/alert-ssdb.pl");
to:
new ProcessBuilder("/usr/bin/perl", "/opt/alert-ssdb.pl");
I've had past experiences where not all my environment variables from the shell exist when using ProcessBuilder.
Edited to reflect #dcsohl's comment.

ProcessBuilder redirecting output

I am trying to redirect output of a process started with the help of ProcessBuilder using following code
ProcessBuilder pb = new ProcessBuilder("/myScript >> /myLogFile 2>&1 <& - &");
Map<String, String> env = pb.environment();
env.clear();
env.put("var1", "val1");
env.put("var2", "val2");
pb.redirectErrorStream(true);
Process p = pb.start();
But it failed with exception
Exception in thread "main"
java.io.IOException: Cannot run
program
"/myScript >>
/myLogFile
2>&1 <& - &": java.io.IOException:
error=2, No such file or directory at
java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
It works fine when I just pass "/myScript"
Script is perl, any suggestions/coments on why it is failing?
I tried passing all of them as seperate arguments like new ProcessBuilder("/myScript",">>","/myLogFile"), it executes but it does not redirect to log file and also it does not take envVars.
Shell redirection operators are unknown to ProcessBuilder. Put your command in a shell script and execute it, as shown here. Alternatively, use bash -c, as shown here.
As you specified, from Java7 you can keep using ProcessBuilder with the only executable file as parameter and redirect/intercept its output stream, using redirectInput() redirectOutput() and redirectError() from ProcessBuilder class.

Categories

Resources