-1

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

Sebi
  • 23
  • 5
  • 3
    This question has been asked (and answered) before, including (among others): [java.io.IOException: Cannot run program "...": java.io.IOException: error=2, No such file or directory](https://stackoverflow.com/questions/16482601/java-io-ioexception-cannot-run-program-java-io-ioexception-error-2-no) Did you search before posting your question for ___ioexception cannot run program___? – Abra Aug 06 '20 at 16:00

1 Answers1

0

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");
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29