JAVA_HOME typically should only include the folder that contains the bin folder.

So in your case

export JAVA_HOME=/home/user/jdk1.7.0_02/

export PATH=$PATH:$JAVA_HOME/bin

In addition for finding the location of your java_home you can follow this command

which java

(This will return the path of the current java binary. Over here its /usr/bin/java)

ls -alh /usr/bin/java

( This will return true path to the symbolic link. Over here its /etc/alternatives/java.

ls -alh /etc/alternatives/java

( This will return true path to this symbolic link which is actual JAVA HOME path)

Answer from gavi on Stack Overflow
🌐
Apple Developer
developer.apple.com β€Ί forums β€Ί thread β€Ί 666681
/usr/libexec/java_home is complete… | Apple Developer Forums
November 14, 2020 - You can get around it by unsetting env var JAVA_HOME before using, e.g. Code Block bashunset JAVA_HOME ; /usr/libexec/java_home -v 1.8 ^ this works for me as I have JDK 8 and 15 installed. ... If you have a oracle JDK or JRE installed shit will hit the fan. I've had the same issue.
🌐
CodeVoila
codevoila.com β€Ί home β€Ί java
How to Uninstall or Remove JDK in Mac OS X - CodeVoila
April 18, 2017 - Tips on how to uninstall JDK or Java SE in Mac OS X. The java_home -v command will list all JDKs already installed in your Mac OS.
Discussions

linux - JAVA_HOME incorrectly set. How to reset it? - Stack Overflow
When I try to run mvn (Apache Maven, that is), I keep getting error "JAVA_HOME" not set. I follow the instructions to set the JAVA_HOME variable as follow; In the terminal: user@localhost$export More on stackoverflow.com
🌐 stackoverflow.com
macos - How to remove an environment variable on OSX using bash - Ask Different
I'm running under Snow Leopard 10.6.8 and I recently added an environment variable which it seems to be messing my bash terminal (I guess). What I did is add the variable DYLD_LIBRARY_PATH=/Library/ More on apple.stackexchange.com
🌐 apple.stackexchange.com
July 17, 2018
java - How to set JAVA_HOME in Mac permanently? - Stack Overflow
I am trying to set JAVA_HOME by entering export JAVA_HOME=/Library/Java/Home at terminal. It sets the JAVA_HOME for current session. How can I set it permanently? More on stackoverflow.com
🌐 stackoverflow.com
unset JDK_HOME as well as JAVA_HOME
JDK_HOME is used by many developer tools to distiguish from the JRE at JAVA_HOME. It would be great if you either unset both, or set both consistently. @dwijnand More on github.com
🌐 github.com
4
January 18, 2016
🌐
javathinking
javathinking.com β€Ί blog β€Ί how-to-uninstall-java-from-mac-completely-jre-and-jdk
How to Uninstall Java Completely from Mac (JRE & JDK): Fix OpenJDK Still Showing in Terminal β€” javathinking.com
If it returns a path (e.g., /Library/Java/JavaVirtualMachines/openjdk-11.0.12.jdk/Contents/Home), unset it temporarily: ... JAVA_HOME and PATH are often set in shell config files like .bash_profile, .bashrc, .zshrc (for Zsh, default in macOS 10.15+), or .profile.
🌐
Igor's Blog
igorkromin.net β€Ί index.php β€Ί 2020 β€Ί 12 β€Ί 16 β€Ί uninstalling-java-on-macos
Uninstalling Java on macOS | Igor Kromin
.zprofile:export JAVA_HOME=`/usr/libexec/java_home -v 1.8` So what I did first was remove that export line from the ~/.zprofile script. Then using the java_home utility, I was able to list all of the java installations that I had... ... 1.8.0_231, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home That's quite a few!
🌐
Milovantomasevic
milovantomasevic.com β€Ί blog β€Ί stackoverflow β€Ί 2021-04-11-how-to-set-java-home-in-mac-permanently
How to set JAVA_HOME in Mac permanently?
April 11, 2021 - Milovan Tomaőević focuses on software development for Enterprise Software Modernization, used by thousands. Expert in data platforms and digital transformation.
Find elsewhere
🌐
Medium
medium.com β€Ί notes-for-geeks β€Ί java-home-and-java-home-on-macos-f246cab643bd
java_home and JAVA_HOME on macOS. How to use java_home and JAVA_HOME with… | by Ross Kendle | Notes for Geeks | Medium
June 1, 2017 - This profile creates aliases to enable interactive setting of the JDK version and sets JAVA_HOME to the default JDK. ... I got the idea for the aliases from https://wimdeblauwe.wordpress.com/2014/03/20/switching-easily-between-java-jdks-on-mac-os-x/.
🌐
GitHub
github.com β€Ί jenv β€Ί jenv β€Ί issues β€Ί 128
unset JDK_HOME as well as JAVA_HOME Β· Issue #128 Β· jenv/jenv
January 18, 2016 - JDK_HOME is used by many developer tools to distiguish from the JRE at JAVA_HOME. It would be great if you either unset both, or set both consistently. @dwijnand
Author Β  jenv
🌐
Mac Install Guide
mac.install.guide β€Ί java β€Ί uninstall
Uninstall Java on Mac Β· Mac Install Guide Β· 2026
Save your current Java configuration before making changes. This helps if you need to restore settings later. Learn How to Open Terminal in Mac and run: $ /usr/libexec/java_home -V > ~/java-backup-info.txt $ echo "JAVA_HOME: $JAVA_HOME" >> ~/java-backup-info.txt
🌐
Mkyong
mkyong.com β€Ί home β€Ί java β€Ί how to set $java_home environment variable on macos
How to Set $JAVA_HOME environment variable on macOS - Mkyong.com
January 19, 2021 - Find out your macOS version. ... For zsh shell, export $JAVA_HOME at ~/.zshenv or ~/.zshrc.
🌐
Oracle
java.com β€Ί mac_uninstall_faq
How do I uninstall Java on my Mac?
Note: To uninstall Java, you must have Administrator privileges and execute the remove command either as root or by using the sudo tool Β· Remove one directory and one file (a symlink), as follows:
🌐
Daml
docs.daml.com β€Ί getting-started β€Ί path-variables.html
Set JAVA_HOME and PATH Variables β€” Daml SDK 2.10.4 documentation
echo 'export JAVA_HOME="$(/usr/libexec/java_home)"' >> ~/.bash_profile echo 'export PATH="$HOME/.daml/bin:$PATH"' >> ~/.bash_profile
Top answer
1 of 14
100

I think JAVA_HOME is the best you can do. The command-line tools like java and javac will respect that environment variable, you can use /usr/libexec/java_home -v '1.7*' to give you a suitable value to put into JAVA_HOME in order to make command line tools use Java 7.

Copyexport JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"

But standard double-clickable application bundles don't use JDKs installed under /Library/Java at all. Old-style .app bundles using Apple's JavaApplicationStub will use Apple Java 6 from /System/Library/Frameworks, and new-style ones built with AppBundler without a bundled JRE will use the "public" JRE in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home - that's hard-coded in the stub code and can't be changed, and you can't have two different public JREs installed at the same time.


Edit: I've had a look at VisualVM specifically, assuming you're using the "application bundle" version from the download page, and this particular app is not an AppBundler application, instead its main executable is a shell script that calls a number of other shell scripts and reads various configuration files. It defaults to picking the newest JDK from /Library/Java as long as that is 7u10 or later, or uses Java 6 if your Java 7 installation is update 9 or earlier. But unravelling the logic in the shell scripts it looks to me like you can specify a particular JDK using a configuration file.

Create a text file ~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf (replace 1.3.6 with whatever version of VisualVM you're using) containing the line

Copyvisualvm_jdkhome="`/usr/libexec/java_home -v '1.7*'`"

and this will force it to choose Java 7 instead of 8.

2 of 14
71

I've been there too and searched everywhere how /usr/libexec/java_home works but I couldn't find any information on how it determines the available Java Virtual Machines it lists.

I've experimented a bit and I think it simply executes a ls /Library/Java/JavaVirtualMachines and then inspects the ./<version>/Contents/Info.plist of all runtimes it finds there.

It then sorts them descending by the key JVMVersion contained in the Info.plist and by default it uses the first entry as its default JVM.

I think the only thing we might do is to change the plist: sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Info.plist and then modify the JVMVersion from 1.8.0 to something else that makes it sort it to the bottom instead of the top, like !1.8.0.

Something like:

Copy<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    ...
    <dict>
            ...
            <key>JVMVersion</key>
            <string>!1.8.0</string>   <!-- changed from '1.8.0' to '!1.8.0' -->`

and then it magically disappears from the top of the list:

Copy/usr/libexec/java_home -verbose
Matching Java Virtual Machines (3):
    1.7.0_45, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
    1.7.0_09, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home
    !1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home

Now you will need to logout/login and then:

Copyjava -version
java version "1.7.0_45"

:-)

Of course I have no idea if something else breaks now or if the 1.8.0-ea version of java still works correctly.

You probably should not do any of this but instead simply deinstall 1.8.0.

However so far this has worked for me.

🌐
Reddit
reddit.com β€Ί r/javahelp β€Ί macos: how to set java_home when both versions are the same
r/javahelp on Reddit: MacOS: How to set java_home when both versions are the same
September 9, 2021 -

On MacOS (I'm using the latest beta but this is a thing in GA code, too), you can use /usr/libexec/java_home -v <version> to choose the JVM.

But both versions the installed JDK's on my machine are 16.0.2 - one ARM version (Azul) and the other x86 (Oracle).

/MyMacBookPro ~ % /usr/libexec/java_home -V Matching Java Virtual Machines (2):

16.0.2 (arm64) "Azul Systems, Inc." - "Zulu 16.32.15" /Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home 

16.0.2 (x86_64) "Oracle Corporation" - "Java SE 16.0.2" /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home 

Is there a way to use /usr/libexec/java_home -v to pick the JVM to use or do I just need to set $JAVA_HOME manually?

Top answer
1 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 4
1
I use jenv. https://www.jenv.be
Top answer
1 of 1
4

$JAVA_HOME will already have a definition on many systems without explicitly declaring it in /etc/environment:

$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle

That said, the unset command will eliminate a variable. From: Unix / Linux - Using Shell Variables

Unsetting Variables

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.

Following is the syntax to unset a defined variable using the unset command βˆ’

unset variable_name

The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works βˆ’

#!/bin/sh

NAME="Zara Ali"
unset NAME
echo $NAME

In our case we can use:

$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle
───────────────────────────────────────────────────────────────────────────────────────────
$ unset JAVA_HOME
───────────────────────────────────────────────────────────────────────────────────────────
$ echo $JAVA_HOME

───────────────────────────────────────────────────────────────────────────────────────────
$ 
🌐
Apple Community
discussions.apple.com β€Ί thread β€Ί 253275045
How to uninstall Java from macbook pro M1… - Apple Community
October 20, 2021 - how do I uninstall (delete) CleanMy Mac ? How do I uninstall "CleanMyMac" on my iMac Catalina? 1878 4 ... Try running the java_home command which should show you where the home folder is located.