Solved ProcessBuilder Help

Discussion in 'Spigot Plugin Development' started by Swedz, Jun 26, 2016.

  1. To run my Hub server, I am using a shell script that is in /root/minecraft/Hub/, and this runs the hub server, and to run that I use cd minecraft/Hub (didn't include /root/ because I'm using the root user), and that runs fine. But in a plugin I've been working on, I'm trying to get it to boot up a specific server at a specific location. I've managed to get a ProcessBuilder (sort of) setup:
    Code (Text):
    File working_dir = new File("/root/minecraft/PlayerServers/" + server + "/");
    if(working_dir.exists()) {
        ProcessBuilder pb = new ProcessBuilder("sh /root/minecraft/PlayerServers/" + server + "/server.sh");
        pb.directory(working_dir);
        pb.start();
    }
    And this runs fine, but I get a "No such file or directory" error in console:
    Code (Text):
    [13:53:13] [Server thread/WARN]: java.io.IOException: Cannot run program "sh /root/minecraft/PlayerServers/PS-1/server.sh" (in directory "/root/minecraft/PlayerServers/PS-1"): error=2, No such file or directory
    [13:53:13] [Server thread/WARN]:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    [13:53:13] [Server thread/WARN]:     at me.Swedz.core.api.PlayerServerAPI.bootup(PlayerServerAPI.java:194)
    [13:53:13] [Server thread/WARN]:     at me.Swedz.core.api.PlayerServerAPI.startup(PlayerServerAPI.java:266)
    [13:53:13] [Server thread/WARN]:     at me.Swedz.core.api.PlayerServerAPI.create(PlayerServerAPI.java:68)
    [13:53:13] [Server thread/WARN]:     at me.Swedz.lobby.Lobby.onInventoryClick(Lobby.java:540)
    [13:53:13] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [13:53:13] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    [13:53:13] [Server thread/WARN]:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [13:53:13] [Server thread/WARN]:     at java.lang.reflect.Method.invoke(Method.java:498)
    [13:53:13] [Server thread/WARN]:     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306)
    [13:53:13] [Server thread/WARN]:     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    [13:53:13] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502)
    [13:53:13] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13)
    [13:53:13] [Server thread/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    [13:53:13] [Server thread/WARN]:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653)
    [13:53:13] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556)
    [13:53:13] [Server thread/WARN]:     at java.lang.Thread.run(Thread.java:745)
    [13:53:13] [Server thread/WARN]: Caused by: java.io.IOException: error=2, No such file or directory
    [13:53:13] [Server thread/WARN]:     at java.lang.UNIXProcess.forkAndExec(Native Method)
    [13:53:13] [Server thread/WARN]:     at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
    [13:53:13] [Server thread/WARN]:     at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    [13:53:13] [Server thread/WARN]:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    [13:53:13] [Server thread/WARN]:     ... 24 more
    This confuses me, because the file clearly DOES exist, and I'm able to access it fine:
    [​IMG]

    Thanks in advance!

    Never mind... Someone at one of my old posts said something that sparked an idea. I'm going to try a Runtime.getRuntime().exec(); with a specified environment.

    I have managed to solve the issue! I am now using:
    Code (Text):
    Runtime.getRuntime().exec("sh /root/minecraft/PlayerServers/" + server + "/server.sh", new String[] {}, new File("/root/minecraft/PlayerServers/" + server + "/"));
     
    #1 Swedz, Jun 26, 2016
    Last edited: Jun 26, 2016