Java launches another Java application

I am going to create a can of wrappers for the can I created. It will handle the update of the main application and make sure that the users are valid users. I have a serious problem, because I can not get the function of starting an external bank. This is what I have 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 get an exception not found in the file.

 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 java -jar ~/Documents.Java/myJar.jar and paste it directly into the terminal, it will work and jar will start. I have no idea what is going on here. Is a path expected relative to the location of a working can?

+6
source share
3 answers

Close call of Java process execution on Linux and the difference between ProcessBuilder and Runtime.exec ()

In addition to the correct tilde pivot points (not), you pass the entire command as a single argument to new ProcesssBuilder . Unlike Runtime.exec() , which treats a single String as a special case and breaks into tokens with space delimiters mainly (but not exactly), like regular Unix shells, ProcessBuilder ctor does not. This can be seen in the exception message at the beginning of the sent trace. You need separate arguments, for example:

 ProcessBuilder builder = new ProcessBuilder("java", "-jar", System.getProperty("user.home")+"/Documents.Java/myJar.jar"); // three String passed to vararg, compiler makes array for you 

or maybe (but I do not recommend)

 String line = "java -jar " + System.getProperty("user.home")+"/Documents.Java/myJar.jar"; ProcessBuilder builder = new ProcessBuilder( line.split(" ") ); // array of three String passed directly to vararg 

And replace java with the full path if the desired java program (or a link to it) was not found first when searching for the PATH valid for your JVM process.

+6
source

Tilda's extension (lead ~ ) is a feature of the shell. You do not call java through the shell, so this does not happen. Use the System.getProperty("user.home") method System.getProperty("user.home") to find the user's home directory and create a command using this instead of the tilde.

+8
source

I have a slightly different idea - of course, Jim is right, because in "~" it will not work inside the process builder; and using system prefs is usually the way out.

In addition, I suggested: just check in advance that any file name that you use on the command line points to an existing file.

Why not create a File object pointing to your JAR? Therefore, you do not need to wait for the appearance of IOExceptions, you make a simple call to exists () to see if the path you compiled for the JAR makes sense. And you can do the same for your java executable!

+1
source

All Articles