2

I need to use ProcessBuilder to invoke java -jar another-program.jar in a Java program. When Java is not in $PATH I will need to find the location of current running Java process, otherwise ProcessBuilder will throw IOException.

One working solution is this:

String javaPath = System.getProperty("java.home") + "/bin/java";

But when I run the Java program with JDK, it will return the Java binary inside the JRE folder instead.

How can I retrieve the correct Java path?

I don't want to rely on JAVA_HOME or /proc/self/exe, hopefully there's a pure Java solution.

Lii
  • 11,553
  • 8
  • 64
  • 88
daisy
  • 22,498
  • 29
  • 129
  • 265
  • If running the other app using the "java -jar" and invoking `main()` is not necessary then maybe this would be helpful https://stackoverflow.com/a/9509608/5872703 – Mouamle Hasan Jun 01 '21 at 03:23
  • 1
    Why can't you use the java binary inside the JRE folder? It works the same as the other one. – Andreas Jun 01 '21 at 04:27
  • https://stackoverflow.com/a/62077059/10871900 might help. – dan1st Jun 01 '21 at 07:13

0 Answers0