Setting the JAVA_HOME
environment variable is the proper way to specify the Java version within a session. You can specify JAVA_HOME
in the info.plist file or by creating a loader application that sets the environment variable before launching the application.
Please note that setting JAVA_HOME
is generally a bad idea because available versions of Java vary depending on the platform/OS version. For example, only 64-bit Intel Macs have Java 6 with Leopard. Both 32-bit and 64-bit Intel Macs have Java 6 with Snow Leopard.
I've found that if you want to support 32-bit Intel Core Duo Macs that have Tiger or Leopard, the best thing to do is target Java 1.5. The Java 1.6 JRE will run Java 1.5 applications. If you need Java 1.6, then you must demand 64-bit Intel with Leopard or any Intel with Snow Leopard from your users. Mind you, there is SoyLatte which provides a Java 1.6 32-bit alternative for Tiger and Leopard users.
"java -version" does change based on the setting of JAVA_HOME
, and, as you noticed, also by Java Preferences.
To sum it up, you shouldn't really set JAVA_HOME
unless you're distributing a Java JRE with your application, such as SoyLatte. In that case you know the target version and path will be there. Otherwise, you can't rely on a particular version being provided by the OS.
You can find the documentation on Apple's Developer website here:
http://developer.apple.com/mac/library/documentation/Java/Conceptual/Java14Development/01-JavaOverview/JavaOverview.html