java – ProcessBuilder在包含本地环境变量的命令上失败
作者:互联网
我可以在Linux上执行通常的命令,由processBuilder包装.但是我正在尝试运行Minecraft服务器,如下例所示,在命令之前设置了一些变量,并且它失败并出现异常.
final ProcessBuilder processBuilder = new ProcessBuilder("LD_LIBRARY_PATH=. ./bedrock_server");
processBuilder.directory(MC_PAL_LOCATION_DIR.toFile());
process = processBuilder.start();
例外:
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)
是否有可能将processBuilder用于此类命令?如果我将它直接粘贴到终端,该命令有效.
链接到服务器:https://minecraft.net/en-us/download/server/bedrock/
命令:LD_LIBRARY_PATH =. ./bedrock_server
解决方法:
如果没有bash,你不能使用这样的bash shell命令.但您可以通过编程方式自行操作环境.喜欢,
final ProcessBuilder processBuilder = new ProcessBuilder("./bedrock_server");
processBuilder.environment().put("LD_LIBRARY_PATH", ".");
processBuilder.directory(MC_PAL_LOCATION_DIR.toFile());
process = processBuilder.start();
标签:java,process,processbuilder 来源: https://codeday.me/bug/20190722/1499919.html