0

From a Java application I want to run another Java application on the same Java installation but in a separate process.

AFAIK for a new process I would use ProcessBuilder to run a command like

java -jar my.jar

but what if java is in a different directory, or should be java.exe since we are on Windows, or java.exe has some other name since the first application was jlinked and jpackaged?

Edit: What I learned meanwhile is that a jpackaged application comes with a native executable that sits in front of the JVM but passes all arguments to the application. That means it is no longer possible to specify an alternative jar to be executed, and some other mechanism is necessary.

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Does this answer your question? [Get full command line from Java](https://stackoverflow.com/questions/72196073/get-full-command-line-from-java) – XtremeBaumer May 11 '22 at 07:22
  • It's my own question, from a different angle. Yes, they are connected but not identical. ;-) – Queeg May 11 '22 at 07:25
  • 1. Shouldn't have asked this question in the first place, as they are looking for the same goal. Quoted question text `My code shall spawn another Java process...points to the currently running executable` to this title `Spawn another process on same JVM`. 2. Should have done some research on your own, as the first result to `Java code find out which and how the running JVM was launched` was the answer – XtremeBaumer May 11 '22 at 07:29
  • Having the first parameter of the command line does not help in case of jlink/jpackage as the executable does not take a typical java command line. Therefore it is unsuitable for running any application other than the packaged one. – Queeg May 11 '22 at 07:37

1 Answers1

0

If jlink image used within jpackage based apps is built without using the --strip-native-commands flag then the runtime image will contain bin/java (or bin/java.exe and bin/javaw.exe on Windows).

This will mean that you should be able to determine a path to launch a new JVM which works inside or outside of jpackage apps just based on the current runtime's java.home and by appending extra VM arguments in normal way for the classpath and main / module of the other application.

On Windows the environment property echo %PATHEXT% usually contains .EXE so the following should specify the java executable for the current runtime without needing to add the correct file extension on Windows:

String java = Path.of(System.getProperty("java.home"),"bin", "java").toString();

You can test above inside your jpackage and non-jpackaged app with a simple one-liner (note that this is not proper way to use ProcessBuilder):

new ProcessBuilder(java, "-version").start().getErrorStream().transferTo(System.out);

Obviously, the above is no help if you wish to determine whether to use Windows console enabled java.exe versus non-console javaw.exe.

DuncG
  • 12,137
  • 2
  • 21
  • 33
  • Your first sentence is wrong. See https://stackoverflow.com/a/71926280/4222206. However using the referenced response plus your answer delivers a good result. Want to edit? – Queeg May 16 '22 at 16:33
  • @Hiran Chaudhuri That's good to know, I shall reword. – DuncG May 17 '22 at 07:00