javasystem-properties

java.version returning surprising value


I am trying to test legacy code under a different JRE than that which it was written for. Running under Mac OS Monterey, 12.7.1. Let's call them JRE_old and JRE_new. To be certain which one my code is running under, I have the following statement in my inits:

System.err.println(System.getProperty("java.version"));

I have tried cding my Terminal to JRE_new/Contents/Home/bin, and launching the java there on my app's jar file (java -jar <path to the jar>), but the version string output is that of JRE_old.

I have also tried setting JAVA_HOME to point to JRE_new/Contents/Home, and double-clicking my app (= MyApp.app), but again, JRE_old's version string is output.

Can someone please tell me where I am going wrong? Thank you! :-)


Solution

  • A somewhat sketchy answer, but since nobody else has tried... If I’m completely wrong, I guess I’ll provoke someone to write a better answer. :-)

    The usual way (I think...) to run Java on macOS is to always use /usr/bin/java and set JAVA_HOME to the version you want; there is usually no need to mess around with PATH. The java installed in /usr/bin is a “shim” that launches the real Java runtime.

    I must confess I do not know what happens when you double-click a .jar file. It is probably not affected by your setting JAVA_HOME, because is is quite tricky to set environment variables in a way that actually affects the Finder (just editing .*shrc won’t do).

    I know even less how a Java program packaged as an app bundle gets launched (and it probably depends on exactly how the packaging was done – I have seen different variants of this). My guess would be that, somewhere inside the MyApp.app bundle, there’s in Info.plist property list file that controls the Java version to use.

    The current working directory (i.e, where you’re cd:d to) when you invoke java has no effect on which implementation gets chosen. (Unless you have . in your PATH, but you haven’t — good!).

    So, my recommendation would be