Tuesday, December 24, 2019

OS/X: setting the default version of Java used

When developing a project it is often convenient to have a specific version of Java associated with JAVA_HOME and to have this version assignment made each time a terminal window is run,.

Setting JAVA_HOME to allow Java development  is not always about the latest and greatest version of Java. One of my current projects is Java 1.8. To configure my Mac accordingly, the first step was to determine  which version or versions of Java were installed.

To do this run a terminal window and from the prompt type:
/usr/libexec/java_home -V

This displays the version of Java available on a Mac:

Version 12.0 was the native version of Java on the Mac and version 1.8, had been installed later. Using Vim, create a .bash_profile file if it does not exist. If this file exists, edit the .bash_profile file. When using Vim to edit, use sudo to insure that the created./edit .bash_profile file can be saved:
sudo vim .bash_profile

The following line needs to be included in .bash_profile in order for version 1.8 to be the default version supported on the machine:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

An example of .bash_profile being edit in VIM is as follows:

Close and save VIM once the change is made to .bash_profile. For the case of creating .bash_profile make sure that this file can be run as an executable:
sudo chmod +x .bash_profile

The sudo command is used before chmod to insure that the appropriate rights available to permit chmod +x being applied to .bash_profile.'

Assign JAVA_HOME using the following from terminal window:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

If .bash_profile is a single line containing "export JAVA_HOME" the commands (or command) in the .bash_profile file can be invoked using:
source .bash_profile

Close the current terminal window and open a new terminal window. In the new terminal window run:
java -version

When the terminal window was created, .bash_profile should have been invoked so it should be no surprise that "java -version" displays the following:

No comments :

Post a Comment