Search code examples
javalinuxbashexecuteprocessbuilder

Linux run other jars


i got a problem. I have a jar file, that should execute a Linux command which starts another jar file.

But it always tells me this if i run my jar:

java.io.IOException: Cannot run program "bash java -jar -Xmx2048M -Xms2G -XX:MaxPermSize=128M spigot.jar nogui" (in directory "/home/RusticGamesNetwork/MinecraftServers/MainLobby"): error=2, File or Directory not found
        at java.lang.ProcessBuilder.start(Unknown Source)
        at de.Cammeritz.RootServer.Servers.lobby(Servers.java:13)
        at de.Cammeritz.RootServer.Main.main(Main.java:11)
Caused by: java.io.IOException: error=2, File or Directory not found
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 3 more

This are my 2 classes:

package de.Cammeritz.RootServer;

public class Main {

public static void main(String args[]) {

    Servers s = new Servers();

    System.out.println("Server werden gestartet!");

    s.lobby();

    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("In 10 sec. schließt sich das Programm selber!");

    try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("Das Programm wird geschlossen!");

    System.exit(0);
    return;
}

}

and this one:

package de.Cammeritz.RootServer;

import java.io.File;
import java.io.IOException;

public class Servers {

public void lobby() {

    ProcessBuilder pb = new ProcessBuilder("bash java -jar -Xmx2048M -Xms2G     -XX:MaxPermSize=128M spigot.jar nogui");
    pb.directory(new File("/home/RusticGamesNetwork/MinecraftServers/MainLobby/"));
    try {
       pb.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

Thank you guys for help :)


Solution

  • The problem is not when running java. Even bash is not being executed. I don't think so that you need to run java from bash at all.

    ProcessBuilder pb = new ProcessBuilder("java", "-jar", "spigot.jar", "-Xmx2048M", "-Xms2G,     "-XX:MaxPermSize=128M", "nogui");
    

    What is happening is that java is trying to run executable named "bash java -jar -Xmx2048M -Xms2G -XX:MaxPermSize=128M spigot.jar nogui" instad of "bash" with parameters.

    See: http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html