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 Overflowlinux - JAVA_HOME incorrectly set. How to reset it? - Stack Overflow
macos - How to remove an environment variable on OSX using bash - Ask Different
java - How to set JAVA_HOME in Mac permanently? - Stack Overflow
unset JDK_HOME as well as JAVA_HOME
Videos
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)
Goto Terminal and open either of the following files using an editor of your choice (vim, nano, etc):
# nano /etc/profile(or)
# nano /root/.bash_profile(Instead of root you can also change your normal username.)
Now run the following commands:
# export JAVA_HOME="/opt/jdk1.6.0" # export PATH="/opt/jdk1.6.0/bin:$PATH"Logout and logon the system , now check the java version in your terminal using the following command:
# java -versionThe output should look similar to this:
# java -version java version β1.6.0β³ Java(TM) SE Runtime Environment (build 1.6.0-b105) Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
You can use /usr/libexec/java_home -v <version you want> to get the path you need for JAVA_HOME. For instance, to get the path to the 1.7 JDK you can run /usr/libexec/java_home -v 1.7 and it will return the path to the JDK. In your .profile or .bash_profile just add
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
and you should be good. Alternatively, try and convince the maintainers of java tools you use to make use of this method to get the version they need.
To open '.bash_profile' type the following in terminal :
nano ~/.bash_profile
and add the following line to the file:
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
Press CTRL+X to exit the bash. Press 'Y' to save changes.
To check whether the path has been added, type following in terminal:
source ~/.bash_profile
echo $JAVA_HOME
I was facing the same issue in MAC Catalina, If I edit .bash_profile i found export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home But When I run terminal echo $JAVA_HOME it was returning empty, Later I found that the file .zshrc was missing I created this file with
touch .zshrc
Then edit it by nano .zshrc and wrote
source ~/.bash_profile
Which solves my issue permanently
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.
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.
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?