java.io.IOException: Cannot run program "java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main": error=2, No such file or directory

huangapple go评论69阅读模式
英文:

java.io.IOException: Cannot run program "java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main": error=2, No such file or directory

问题

I've tried to restart my Java Application with this code:

public static void restartApplication() throws URISyntaxException, IOException {
    final File currentJar = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());

    /* is it a jar file? */
    if (!currentJar.getName().endsWith(".jar"))
        return;

    /* Build command: java -jar application.jar */
    final ProcessBuilder builder = new ProcessBuilder("java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main");
    builder.inheritIO();
    builder.start();

    System.exit(0);
}

When I type the command into my command panel, it works totally fine.
So what is the error here?
Output

java.io.IOException: Cannot run program "java -Xmx512m -cp
/opt/Nils.jar dev.keksstudios.core.Main": error=2, No such file or
directory

英文:

I've tryied to restart my Java Application with this code:

public static void restartApplication() throws URISyntaxException, IOException {
    final File currentJar = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());

    /* is it a jar file? */
    if(!currentJar.getName().endsWith(".jar"))
        return;

    /* Build command: java -jar application.jar */
    final ProcessBuilder builder = new ProcessBuilder("java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main");
    builder.inheritIO();
    builder.start();

    System.exit(0);
}

When I type the command into my command panel it works totaly fine.
So what is the Error here?
Output

> java.io.IOException: Cannot run program "java -Xmx512m -cp
> /opt/Nils.jar dev.keksstudios.core.Main": error=2, No such file or
> directory

答案1

得分: 0

>final ProcessBuilder builder = new ProcessBuilder("java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main");

这行是错误的。Java/操作系统会寻找名为 java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main.exe 的可执行文件。你需要将参数分开写,像这样:

final ProcessBuilder builder = new ProcessBuilder("java", "-Xmx512m" ,"-cp", "/opt/Nils.jar", "dev.keksstudios.core.Main");
英文:

>final ProcessBuilder builder = new ProcessBuilder("java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main");

This line is false. Java/The OS will look for an executable called java -Xmx512m -cp /opt/Nils.jar dev.keksstudios.core.Main.exe.
You have to separate the arguments like this:

final ProcessBuilder builder = new ProcessBuilder("java", "-Xmx512m" ,"-cp", "/opt/Nils.jar", "dev.keksstudios.core.Main");

huangapple
  • 本文由 发表于 2020年8月6日 23:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63287347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定