Java starts another Java application
I'm building a wrapper jar for the jar I built It will handle updating the main application and ensure that the user is a valid user I have a major problem because I can't make the external jar startup function work properly This is what I have done so far:
ProcessBuilder builder = new ProcessBuilder("java -jar ~/Documents.Java/myJar.jar"); try { Process process = builder.start(); } catch (Exception e) { e.printStackTrace(); }
However, I just got a file and found no exception
java.io.IOException: Cannot run program "java -jar ~/Documents/Java/myJar.jar": error=2,No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at com.mycompany.DHSLauncher.Launcher.lambda$4(Launcher.java:109) at java.util.Optional.ifPresent(Optional.java:159) at com.mycompany.DHSLauncher.Launcher.showLogin(Launcher.java:102) at com.mycompany.DHSLauncher.Launcher.start(Launcher.java:35) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.invokelaterDispatcher$Future.run(invokelaterDispatcher.java:95) Caused by: java.io.IOException: error=2,No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:248) at java.lang.ProcessImpl.start(ProcessImpl.java:134) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) ... 10 more
If I copy Java - jar ~ / documents Java / myJar. Jar and paste it into the terminal, it can run and start jar I don't know what's going on here Should this path be relative to the running jar?
Solution
Close to dupe Java execute process on Linux and difference between processbuilder and runtime exec()
In addition to the correct point about the tilde (non) extension, you pass the entire command line as a parameter to the new processsbuilder And runtime Unlike exec(), runtime Exec () treats a single string as a special case, and most (but not all) are split into space separated tags like a typical UNIX shell, which processbuilder ctor does not do This can be seen in the exception message you posted at the beginning of backtracking You need separate parameters, such as:
ProcessBuilder builder = new ProcessBuilder("java","-jar",System.getProperty("user.home")+"/Documents.Java/myJar.jar"); // three String's passed to vararg,compiler makes array for you
Or maybe (but I don't recommend it)
String line = "java -jar " + System.getProperty("user.home")+"/Documents.Java/myJar.jar"; ProcessBuilder builder = new ProcessBuilder( line.split(" ") ); // array of three String's passed directly to vararg
If you can't find the required Java program (or its link) when searching for a path valid for your JVM process, replace Java. Net with the full pathname