我可以在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
答案 0 :(得分:6)
没有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();
答案 1 :(得分:4)
正如@ElliottFrisch指出的那样,没有bash就不能使用shell命令,因此,您要向环境映射添加LD_LIBRARY_PATH或执行bash:
final ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "LD_LIBRARY_PATH=. ./bedrock_server");
processBuilder.directory(MC_PAL_LOCATION_DIR.toFile());
process = processBuilder.start();