Linux运行其他jar

时间:2014-09-06 18:45:12

标签: java linux bash execute processbuilder

我遇到了问题。 我有一个jar文件,它应该执行一个启动另一个jar文件的Linux命令。

但如果我运行我的罐子,它总是告诉我这个:

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

这是我的两个班级:

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;
}

}

和这一个:

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();
    }

}
}

谢谢你们的帮助:)

1 个答案:

答案 0 :(得分:1)

运行java时问题不是。即使是bash也没有被执行。我不这么认为你需要从bash运行java。

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

正在发生的事情是java正在尝试运行名为&#34; bash java -jar -Xmx2048M -Xms2G -XX:MaxPermSize = 128M spigot.jar nogui&#34; &#34; bash&#34;带参数。

请参阅:http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html