This configuration is useful when you decided to change your default shell in OS X, for example in our case we have Fish shell and it will not have the JAVA_HOME set out-of-the-box.
How can we test if we have configured this environment variable? Easy, just type:
echo $JAVA_HOME
If the output is empty, then we don’t have the variable in place.
Step 1. Find and open your configuration file
If you are using Fish shell, chances are you are also using Oh My Fish which provides infrastructure to allow you to install packages to extend and modify the look and feel of your shell. Let’s open the configuration file then:
nano ~/.config/fish/conf.d/omf.fish
The “~/” is a shortcut for accessing the home directory of your user.
Step 2. Locate your Java directory where the Java Development Kit was installed
If you don’t know where the Java Development Kit (JDK) was installed, just run:
sudo find / -name javac
The output may be similar to this:
/usr/bin/javac /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/javac /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/javac find: /private/var/db/ConfigurationProfiles/Store: Operation not permitted find: /private/var/folders/y5/abbasdf/0/SafariFamily: Operation not permitted
Notice the second line, that is our JDK installation directory, so the directory we need to use is this one: /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/
Step 3. Add the JAVA_HOME to the Oh My Fish configuration file
Let’s open the file:
nano ~/.config/fish/conf.d/omf.fish
Add the JAVA_HOME variable at the end of ~/.config/fish/conf.d/omf.fish
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/"
Save the file ~/.config/fish/conf.d/omf.fish (in nano just press Control + O followed by Control + X to close the file). Finally, let’s review or configuration by typing:
echo $JAVA_HOME
This command, should now print the correct JAVA_HOME:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/
Sweet, isn’t it?
See you next time.
Alex Arriaga
You can use `/usr/libexec/java_home/bin/java` tool,
e.g.
set -gx JAVA_HOME (/usr/libexec/java_home -F)