Setting JAVA_HOME and JRE_HOME path - java

I have been allocated a Linux box in which has java available
# java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.4.1.el6_3-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)
# ls -l /usr/bin/java
lrwxrwxrwx. 1 root root 22 Feb 8 2013 /usr/bin/java -> /etc/alternatives/java
I am new to Java and not sure if JRE is installed in ths box but based upon search:
# rpm -q jre
package jre is not installed
# find / -iname java -print 2>/dev/null
/usr/lib/java
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/jre/bin/java
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java
/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre/bin/java
/usr/bin/java
/usr/share/java
/var/lib/alternatives/java
/etc/alternatives/java
/etc/java
/etc/pki/java
But
# echo $JAVA_HOME
# echo $JRE_HOME
So is JAVA is installed and JRE also - am I correct but what value I should set for JAVA_HOME and JRE_HOME env variables?

I am not sure about any other version of Linux, but in Ubuntu you need open home/.bashrc and add in the end of file
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64
export PATH=$JAVA_HOME/bin:$PATH
after that, save file and relogin.
EDIT
java will know where is jre by JAVA_HOME.

firstly try to get out of root user if possible than after that change below in your ~/.bash_profile
JAVA_HOME=/usr/java/<Java version 7 jdk>; export JAVA_HOME // you can also try JAVA_HOME=/usr/java/jdk 7 version/bin/java
PATH=$JAVA_HOME/bin:$PATH; export PATH
save it and then
now source ~/.bashrc
after that try
echo $JAVA_HOME it will produce the expected result.

JAVA_HOME should be set as parent folder of the java installed path
and JRE_HOME should be /jre

set $JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64
set $JRE_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre

Based on the output you have posted, i can draw the following conclusions:
Based on the fact that java -version works on your machine, you have the JRE installed.
The 'echo' statements do not produce any output as you have not set the environment variables for them.
On a side note, if you are planning on doing Java Development, then you need the JDK installed. The JRE comes as a subset of the JDK once installed

Related

Java version can't be switch in MAC os it always set 1.6.0 like default [duplicate]

How can you change the default version of Java on a mac?
First run /usr/libexec/java_home -V which will output something like the following:
Matching Java Virtual Machines (3):
1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
or you can specify just the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now when you run java -version you will see:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Add the export JAVA_HOME… line to your shell’s init file.
For Bash (as stated by antonyh):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
For Fish (as stated by ormurin)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Updating the .zshrc file should work:
nano ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
Press CTRL+X to exit the editor
Press Y to save your changes
source ~/.zshrc
echo $JAVA_HOME
java -version
This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).
As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.
The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).
Solution without 3rd party tools:
leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.
System launcher will use the JDK with highest version among those that have an Info.plist file.
When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).
Details of investigation in this gist.
(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.
Adding to the above answers, I put the following lines in my .bash_profile (or .zshrc for MacOS 10.15+) which makes it really convenient to switch (including #elektromin's comment for java 9):
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
After inserting, execute $ source .bash_profile
I can switch to Java 8 by typing the following:
$ j8
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
A small fish function based on /usr/libexec/java_home
function jhome
set JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
If you don't use fish, you can do something similar in bash:
#!/bin/bash
jhome () {
export JAVA_HOME=`/usr/libexec/java_home $#`
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
}
Then to switch between javas do:
$> jhome #switches to latest java
$> jhome -v 1.7 #switches to java 1.7
$> jhome -v 1.6 #switches to java 1.6
ref: https://gist.github.com/kenglxn/1843d552dff4d4233271
Update...
With macOS Monterey, v12.0.1 Cask is no longer a Brew command. Just follow the steps but in step 4 just do $ brew install ... or $ brew install --cask ... instead of $ brew cask install ... Thanks to #Shivam Sahil comment.
I will share my experiences with macOS Big Sur v11.4, the best way to deal with these problems is by installing java using Homebrew:
1 - Install Homebrew.
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2 - Install Homebrew Cask.
$ brew tap homebrew/cask-versions
$ brew update
$ brew tap homebrew/cask
3 - Install the latest version of Java
$ brew cask install java
4 - Install the other needed versions of Java (Java8, Java11, Java13).
$ brew tap adoptopenjdk/openjdk
$ brew cask install adoptopenjdk8
$ brew cask install adoptopenjdk11
$ brew cask install adoptopenjdk13
$ brew cask install adoptopenjdk14
5 - Switch between different versions of Java
Switching between different versions of Java, you only need to add the following to your .bash_profile or .zshrc.
In this case, we want to be able to switch between Java8, Java11, Java13 and Java14:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java13='export JAVA_HOME=$JAVA_13_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
# default to Java 14
java14
6 - Reload .bash_profile or .zshrc for the aliases to take effect:
$ source ~/.bash_profile
or
$ source ~/.zshrc
7 - Finally you can use the aliases to switch between different Java versions.
$ java8
$ java -version
java version "1.8.0_261"
For more info you can see this post: How to Use Brew to Install Java on Mac
Use jenv is an easy way. (Update 2022)
Install jenv: see Getting started
Install java with brew
brew install openjdk#11
ln -s /usr/local/opt/openjdk#11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
#other java
brew install openjdk#8
brew install openjdk#17
Add java to jenv
jenv add /Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home
Use: refer to jenv
It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks). Basically, you gonna have to update your alias to java.
Step by step:
After installing JDK 1.7, you will need to do the sudo ln -snf in order to change the link to current java. To do so, open Terminal and issue the command:
sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents \
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
Note that the directory jdk1.7.0_51.jdk may change depending on the SDK version you have installed.
Now, you need to set JAVA_HOME to point to where jdk_1.7.0_xx.jdk was installed. Open again the Terminal and type:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
You can add the export JAVA_HOME line above in your .bashrc file to have java permanently in your Terminal
install JDK, not just JRE
/usr/libexec/java_home -v 1.8
gives
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
next
touch .bash_profile
open -a TextEdit.app .bash_profile
TextEdit will show you a blank page which you can fill in.
add to doc:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
in terminal:
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
try the command:
javac -version
should output:
javac 1.8.0_111
tl;dr
Add the line:
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home'
to the file
~/.bash_profile
(replace jdk1.8.0_144.jdk with your downloaded version)
then source ~/.bash_profile
Use jenv, it is like a Java environment manager. It is super easy to use and clean
For Mac, follow the steps:
brew install jenv
git clone https://github.com/gcuisinier/jenv.git ~/.jenv
Installation: If you are using bash follow these steps:
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l
Add desired versions of JVM to jenv:
jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
jenv add /System/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home
Check the installed versions:
jenv versions
Set the Java version you want to use by:
jenv global oracle64-1.6.0
Four easy steps using terminal for people who uses the default process.. :)
echo $JAVA_HOME gives you current java home. For eg: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/
cd /Library/Java/JavaVirtualMachines/ will take you to the folder where u normally install jdks (It might be different for your machines)
ls shows you available folders (normally it will have the version numbers, for eg: jdk1.8.0_191.jdk openjdk-11.0.2.jdk)
export JAVA_HOME='/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home' will change the java home..
Consider the following approach only to change the JDK for each and specific tab of your terminal (i.e: iTerm).
Having in the /Library/Java/JavaVirtualMachines path the two following jdks
openjdk8u275-b01
openjdk-11.0.9.1+1
And in the .bash_profile file the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-11.0.9.1+1/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
If you open Iterm - with the first Tab A - and the following commands are executed:
javac -version
javac 11.0.9.1
java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)
The output is correct and expected
But if you open a second Tab B and you need override the default JDK then write in the terminal the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk8u275-b01/Contents/Home/
export PATH=$JAVA_HOME/bin:$PATH
Then
javac -version
javac 1.8.0_275
java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.275-b01, mixed mode)
It works fine. Of course if the Tab B is closed or you open a new Tab C all work according the .bash_profile settings (therefore the default settings)
macOS El Capitan or newer will choose the higher version of the JDK available in /Library/Java/JavaVirtualMachines, so in order to downgrade you may rename the file Info.plist to something else like Info.plist.disabled so that the OS will choose a previous version.
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
put this in your ~/.profile and use it in your terminal like so setjdk 1.8, setjdk 1.7, setjdk 9 etc etc...
If you don't have removeFromPath then it is:
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
You can add it to your .bash_profile to have the version set by default.
//Open bash profile
open ~/.bash_profile
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
// run bash profile
source ~/.bash_profile
add following command to the ~/.zshenv file
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Add following in your ~/.bash_profile and set the default java version accordingly.
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
# default to Java 8
java8
I am using macOS and have installed java using brew.
Edit:
If you are not using bash please update the correct shell file, example, if you are using zsh then it will be ~/.zshrc
Add the following line of code to your .zshrc (or bash_profile):
alias j='f(){ export JAVA_HOME=/usr/libexec/java_home -v $1 };f'
Save to session:
$ source .zshrc
Run command (e.g. j 13, j14, j1.8...)
$ j 1.8
Explanation
This is parameterised so you do not need to update the script like other solutions posted. If you do not have the JVM installed you are told. Sample cases below:
/Users/user/IDE/project $ j 1.8
/Users/user/IDE/project $ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
/Users/user/IDE/project $ j 13
/Users/user/IDE/project $ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
/Users/user/IDE/project $ j 1.7
Unable to find any JVMs matching version "1.7".
Check Java version: java -version
Switch between versions: https://devqa.io/brew-install-java/
open ~/.bash_profile
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
source ~/.bash_profile
When we are switching to java11 or java8, java -version command is not showing the correct version.
In that case use mvn -version to see the correct java version is used for building the solution
From the Apple's official java_home(1) man page:
**USAGE**
/usr/libexec/java_home helps users set a $JAVA_HOME in their login rc files, or provides a way for
command-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-
tecture requirement. The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,
which is useful for starting Java command-line tools from launchd plists without hardcoding the full
path to the Java command-line tool.
Usage for bash-style shells:
$ export JAVA_HOME=`/usr/libexec/java_home`
Usage for csh-style shells:
% setenv JAVA_HOME `/usr/libexec/java_home`
JDK Switch Script
I have adapted the answer from #Alex above and wrote the following to fix the code for Java 9.
$ cat ~/.jdk
#!/bin/bash
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version $1";
requestedVersion=$1
oldStyleVersion=8
# Set the version
if [ $requestedVersion -gt $oldStyleVersion ]; then
export JAVA_HOME=$(/usr/libexec/java_home -v $1);
else
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
fi
echo "Setting JAVA_HOME=$JAVA_HOME"
which java
java -version;
}
Switch to Java 8
$ jdk 8
Switching java version 8
Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
/usr/bin/java
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Switch to Java 9
$ jdk 9
Switching java version 9
Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
/usr/bin/java
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
If still u are not able to set it. using this command.
export JAVA_HOME=/usr/libexec/java_home -v 1.8
then you have to use this one.
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
it will surely work.
First find out where do you store the environment variables-
emacs
bash_profile
zshrc file
Steps to Set up the environment variable :-
Download the jdk from JAVA
install it by double click
Now set-up environment variables in your file
a. For emacs.profile you can use this link OR see the screenshot below
b. For ZSH profile setup -
1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
2. source ~/.zshrc - Restart zshrc to reflect the changes.
3. echo $JAVA_HOME - make sure path is set up properly
----> /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
4. java -version
--> java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b16)Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
All set Now you can easily upgrade or degrade the JAVA version..
Previously I used alias'es in .zshrc for easy switching between versions but today I use SDKMAN.
SDKMAN can also handle setting default java for the system, and downloading and installing new java versions.
Once sdkman is installed you can then do commands similar to what is possible with the nvm tool for handling node versions.
sdk list java will list the java versions available on your system as well as available online for installation including their identifier that you can use in the sdk use, sdk default and sdk install commands.
e.g. to install Amazon Corretto 11.0.8 and ask if it should be the new default do this:
sdk install java 11.0.8-amzn
A feature I also use regularly is the .sdkmanrc file.
If you place that in a directory on your machine and run the sdk env command in the directory then you can configure tool versions used only in that directory. It is also possible to make sdkman switch tool versions automatically using the sdkman_auto_env=true configuration.
sdkman also supports handling other tools for the JVM such as gradle, kotlin, maven and more.
For more information check out https://sdkman.io/usage#env
With no additional tools to install, an easy way to have a separately installed JDK recognized as a first class member by /usr/libexec/java_home -V is to symlink it as follows:
sudo ln -s <path> /Library/Java/JavaVirtualMachines/jdk-[some-identifier].jdk
<path> is expected to be a directory containing subdirectories Contents/Home/bin etc.
A common use case is to register the JDK/JRE included with Android Studio:
The default location for the Java tools in recent versions of Android Studio on macOS is /Applications/Android\ Studio.app/Contents/jre. We will use jdk-android-studio.jdk as the identifier:
sudo ln -s /Applications/Android\ Studio.app/Contents/jre /Library/Java/JavaVirtualMachines/jdk-android-studio.jdk
Now, /usr/libexec/java_home -V will list it under Matching Java Virtual Machines:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
11.0.13 (arm64) "JetBrains s.r.o." - "OpenJDK 11.0.13" /Applications/Android Studio.app/Contents/jre/Contents/Home
/Applications/Android Studio.app/Contents/jre/Contents/Home
If you are using fish and you are using mac and you want to be able to switch between JDK's, then below works for me on mac.
#kenglxn's answer didn't work for me and I figured out it bcos didn't set -g which is global !
Put below under ~/.config/fish/config.fish
alias j8="jhome -v 1.8.0_162"
alias j9="jhome -v 9.0.1"
function jhome
set -g -x JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
funcsave jhome
To know which version /minor version you have installed, you can do :
/usr/libexec/java_home -V 579ms  Wed 14 Feb 11:44:01 2018
Matching Java Virtual Machines (3):
9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
1.8.0_162, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
1.8.0_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
If you have multiple versions and you want to run something by using a specific version, use this example:
/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar
Based on #markhellewell answer I created a couple of alias functions that will do it for you. Just add these to your shell startup file
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version";
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
java -version;
}
https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4
Here is how I do it on my Linux (Ubuntu / Mint mate), I guess Mac can do it similarly.
Install & config
Steps:
[Basic - part]
Download jdk (the .tgz file) by hand.
Uncompress & rename properly, at a proper location.
e.g /mnt/star/program/java/jdk-1.8
Make a soft link, which will be changed to switch java version later.
e.g ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
Thus /mnt/star/program/java/java is the soft link.
Set JAVA_HOME in a start script.
Could use file like /etc/profile.d/eric.sh, or just use ~/.bashrc.
e.g JAVA_HOME=/mnt/star/program/java/java
Then open a new bash shell. java -version should print the java version.
[More version - part]
Download & install more Java version, as need, similar as above steps.
e.g
/mnt/star/program/java/jdk-11
[Switch - part]
In ~/.bashrc, define variable for various Java version.
e.g
_E_JAVA_HOME_11='/mnt/star/program/java/jdk-11'
_E_JAVA_HOME_8='/mnt/star/program/java/jdk-8'
# dir of default version,
_E_JAVA_HOME_D=$_E_JAVA_HOME_8
In ~/.bashrc, define command to switch Java version.
e.g
## switch java version,
alias jv11="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_11 $JAVA_HOME"
alias jv8="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_8 $JAVA_HOME"
# default java version,
alias jvd="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_D $JAVA_HOME"
alias jv="java -version"
In terminal, source ~/.bashrc to make the changes take effect.
Then could switch using the defined commands.
Commands - from above config
Commands:
jv11
Switch to Java 11
jv8
Switch to Java 8
jvd
Switch to default Java version, which is denoted by _E_JAVA_HOME_D defined above.
jv
Show java version.
Example output:
eric#eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric#eric-pc:~$ jv11
eric#eric-pc:~$ jv
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
eric#eric-pc:~$ jvd
eric#eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric#eric-pc:~$
Mechanism
It switch by changing the soft link, which is used as JAVA_HOME.
Tips
On my machine when install jdk by hand, I keep the minor version, then make a soft link with the major version but without the minor version.
e.g
// this is the actual dir,
jdk1.8.0_191
// this is a soft link to jdk1.8.0_191
jdk-8
// this is a soft link to jdk-8 or jdk-11
java
I define command alias in ~/.bashrc, but define variable in a separate file.
I am using ~/.eric_var to define the variables, and ~/.bashrc will source it (e.g source $HOME/.eric_var).
add this function to bashrc or zshrc, java-change [version] to choose the JDK
# set and change java versions
function java-change() {
echo "----- old java version -----"
java -version
if [ $# -ne 0 ]; then
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
echo "----- new java version -----"
java -version
}

(Mac OS) JAVA version does not change

I have 2 JAVA version on my Macbook. I want to change version from 14 to 11. I found 2 solutions on the internet but both are not working.
My java -version result is
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+14)
OpenJDK 64-Bit Server VM (build 14.0.1+14, mixed mode, sharing)
1st Solution (How to set or change the default Java (JDK) version on OS X?),
I run /usr/libexec/java_home -V then I got a result below,
Matching Java Virtual Machines (2):
14.0.1, x86_64: "OpenJDK 14.0.1" /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
11.0.8, x86_64: "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
Then I did
export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`
After checking from java -version, The result is still 14.0.1
2nd solution (How to set JAVA_HOME in Mac permanently?), I edit ~/.bash_profile file as below,
export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`
And I run command
source ~/.bash_profile
echo $JAVA_HOME
It shows the result,
/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
But when I check the result with java -version, It's still 14.0.1 as well
Install sdkman which takes care of the rather tedious command line voodoo you have to employ to try to make this happen. The problem is, JAVA_HOME is just an environment variable, it changes nothing - only tools that explicitly look for it (generally, maven and ant for example) will be affected by messing with it. When you type java on a mac, it runs /usr/bin/java, which is not a file you can change even as root. This java will then invoke the real java, and does not look at JAVA_HOME to get the job done: It is a softlink to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java, and because it is in /System you can't change that either, not even as root.
That's why this is so hard, and why you want a nice tool (sdkman) to do it for you.
The removal of quotes around the JAVA_HOME and then setting the path variable variable worked for me (Mac OS Catalina):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
If you need to switch between versions and already have the JDKs installed, you can adjust your bash profile with aliases that reset JAVA_HOME and reinitialize your PATH each time:
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
alias java11='export JAVA_HOME=$JAVA_11_HOME; export PATH=$JAVA_HOME/bin:$PATH'
alias java14='export JAVA_HOME=$JAVA_14_HOME; export PATH=$JAVA_HOME/bin:$PATH'
Then, after restarting your terminal session, you can switch back and forth:
$ java11
$ java -version
and
$ java14
$ java -version
Try export JAVA_HOME='/usr/libexec/java_home -v 11' (without the minor version), it outputs the right java -version for me on Mac.

How can I set classpath in Mac Os for Selenium webdriver [duplicate]

Java is an optional package on the latest versions of macOS.
Yet once installed it appears like the JAVA_HOME environment variable is not set properly.
With the Java optional package or Oracle JDK installed,
adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.
export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"
Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk for the above to work
Update: added -v flag based on Jilles van Gurp response.
/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.
Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/
At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.
Update: Understanding Java From Command Line on OSX has a better explanation on how this works.
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
is the way to do it. Note, updating this to 1.8 works just fine.
For me, Mountain Lion 10.8.2, the solution most voted does not work.
I installed jdk 1.7 via Oracle and maven from homebrew.
My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too.
I add the below sentence in ~/.bash_profile, and it works.
export JAVA_HOME="$(/usr/libexec/java_home)"
This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.
None of the above answers helped me. I suppose all the answers are for older OS X
For OS X Yosemite 10.10, follow these steps
Use your favorite text editor to open: ~/.bash_profile
//This command will open the file using vim
$ vim ~/.bash_profile
Add the following line in the file and save it ( : followed by "x" for vim):
export JAVA_HOME=$(/usr/libexec/java_home)
Then in the terminal type the following two commands to see output:
$ source ~/.bash_profile
$ echo $JAVA_HOME
In the second line, you are updating the contents of .bash_profile file.
Update for Java 9 and some neat aliases.
In .bash_profile:
export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`
Note, that for the latest version it is 9 and not 1.9.
Set active Java:
export JAVA_HOME=$JAVA_HOME8
export PATH=$JAVA_HOME/bin:$PATH
Some additional alias to switch between the different versions:
alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'
Test in terminal:
% j8
% java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
% j9
% java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
EDIT: Update for Java 10
export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'
EDIT: Update for Java 11
export JAVA_HOME11=`/usr/libexec/java_home --version 11`
alias j11='export JAVA_HOME=$JAVA_HOME11; export PATH=$JAVA_HOME/bin:$PATH'
The above didn't work for me with Amazon's EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.
For OS X you can do:
export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
On Mac OS X Lion, to set visualgc to run, I used:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
The following worked for me. I'm using ZSH on OSX Yosemite with Java 8 installed.
The following command /usr/libexec/java_home emits the path to JDK home:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
In your ~/.zshrc,
export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"
for macOS Mojave 10.14.1 and JAVA 11.0.1
I set the profile as
export JAVA_HOME=$(/usr/libexec/java_home)
key in terminal this to confirm:
$JAVA_HOME/bin/java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
A better (more upgradable) way is to use the following:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
This should work with AWS also since it has bin underneath Home
Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
My approach is:
.bashrc
export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=$JAVA6_HOME
# -- optional
# export PATH=$JAVA_HOME/bin:$PATH
This makes it very easy to switch between J6 and J7
I Had to explicitly set it to the exact path on my Macbook air .
Steps followed:
try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
Edit the Bash p with - sudo nano ~/.bash_profile
Add the exact path to JAVA Home (with the path from step 2 above)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
Save and exit
Check JAVA_Home using - echo $JAVA_HOME
I am running MACOS MOJAVE - 10.14.2 (18C54) on a Macbook Air with JAVA 8
OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:
$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin
and in ~/.zshrc change JAVA_HOME to
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"
For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.
I am sure there is a better way but this got me up and running.
hughsmac:~ hbrien$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
For Java 11 (JDK 11) it can be located with the following command:
/usr/libexec/java_home -v 11
Got the same issue after I upgrade my Mac OS and following worked for me:
cmd>vi ~/.bash_profile
Add/update the line for JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home"
cmd>source ~/.bash_profile or open a new terminal
I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/
If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.
What you do is to edit your ~/.bash_profile and add the following:
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.7
What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using
setjdk 1.7
which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute
setjdk 1.7.0_51
instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.
P.S. Do not forget to source ~/.bash_profile after you save it.
For Fish terminal users on Mac (I believe it's available on Linux as well), this should work:
set -Ux JAVA_8 (/usr/libexec/java_home --version 1.8)
set -Ux JAVA_12 (/usr/libexec/java_home --version 12)
set -Ux JAVA_HOME $JAVA_8 //or whichever version you want as default
This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.
So, I set JAVA_HOME=/usr in my .bashrc.
Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.
Update
As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.
for mac user .
java 8 should add
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
java 6 :
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297
For Mac Yosemite,
JDK 1.7.0_xx is using
$ ls -ltar /usr/bin/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
Anyone using AUSKEY from the Australian Tax Office (ATO) should uninstall AUSKEY.
This sorted out my JAVA_HOME issues.
It is also no longer required for MAC users. Yah!
I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.
It's not automatically set up so to set it correctly on my system I run:
set -U JAVA_HOME (/usr/libexec/java_home)
Just set java_home of 1.8 jdk version in netbeans.conf file:
/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf
uncomment line:
netbeans_jdkhome="path/to/jdk"
and set path to your 1.8 jdk, in my case:
netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"
This approach lays you to have several jdk versions on mac os
Handy command for knowing java home and other details.
java -XshowSettings:properties -version
and to know all java homes on your mac:
/usr/libexec/java_home -V

Ionic Build can't find JDK set in JAVA_HOME even though it is the only Java install

When I installed Java JDK 1.8.x it initially was working, when I rebooted the next day after shutting down over night I get this when I build:
$ ionic build android
Updated the hooks directory to have execute permissions
Running command: d:\installs\nodejs\node.exe d:\htdocs\publicworks\mobile\hooks\after_prepare\010_add_platform_class.js d:/htdocs/publicworks/mobile
add to body class: platform-android
Running command: d:\htdocs\publicworks\mobile\platforms\android\cordova\build.bat
ANDROID_HOME=c:\Users\mtpultz\AppData\Local\Android\android-sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_45\bin
Running: d:\htdocs\publicworks\mobile\platforms\android\gradlew cdvBuildDebug -b d:\htdocs\publicworks\mobile\platforms\android\build.gradle -Dorg.gradle.daemon=true
ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\Java\jdk1.8.0_45\bin
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
d:\htdocs\publicworks\mobile\platforms\android\cordova\node_modules\q\q.js:126
throw e;
^
Error code 1 for command: cmd with args: /s /c "d:\htdocs\publicworks\mobile\platforms\android\gradlew cdvBuildDebug -b d:\htdocs\publicworks\mobile\platforms\android\build.gradle -Dorg.gradle.daemon=true"
But Java is most definitely installed in this directory and nowhere else??? In my terminal:
java -version
Produces the proper output for my JDK:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
The environment variables are set to:
JAVA_HOME =
C:\Program Files\Java\jdk1.8.0_45\bin
and the path contains:
D:\installs\nodejs;
C:\Users\mtpultz\AppData\Roaming\npm;
D:\installs\apache-ant\bin;
C:\Users\mtpultz\AppData\Local\Android\android-sdk\tools;
C:\Users\mtpultz\AppData\Local\Android\android-sdk\platform-tools;
Any suggestions?
JAVA_HOME should be set to the home directory of java and not to bin. So replace this:
C:\Program Files\Java\jdk1.8.0_45\bin
with
C:\Program Files\Java\jdk1.8.0_45
java/bin dir is added to PATH system variable to run java from anywhere.
Do one thing. This will Help You
Remove "\bin" from your JAVA_HOME path. Mean give path only till.
C:\Program Files\Java\jdk1.8.0_45
It will work with you.
For Linux user:
$sudo gedit $HOME/.bashrc
write at end
#Android Path
export ANDROID_HOME=${HOME}/Android/Sdk
export PATH=${PATH}:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools
export PATH
#JAVA
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/
export JAVA_HOME
$source ~/.bashrc
after that open
$sudo gedit /etc/environment
and add at end
JAVA_HOME="/usr/lib/jvm/java-8-oracle/"
$source /etc/environment
after wasting 3 days finally i got some solutions. these are definitely works when you found these errors.
1.Error: JAVA_HOME is set to an invalid directory
JAVA_HOME should be set to the home directory of java and not to bin. So replace this:
C:\Program Files\Java\jdk1.8.0_45\bin
with
C:\Program Files\Java\jdk1.8.0_45
java/bin dir is added to PATH system variable to run java from anywhere
Failed to install 'cordova-plugin-whitelist': Error using forcedroid command line
check your environments variables
1. android platforms tools
2. android tools
3. sdk location
4. git location
cordova - Error: Failed to fetch platform android
the only solution is
cordova platform add android --nofetch
Make sure you use the Java SE SDK and not the Java EE SDK, that solved the problem for me.
Make sure that your JAVA_HOME is having the following path and install JDK SE.
C:\Program Files\Java\jdk1.8.0_131

Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?

Java is an optional package on the latest versions of macOS.
Yet once installed it appears like the JAVA_HOME environment variable is not set properly.
With the Java optional package or Oracle JDK installed,
adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.
export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"
Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk for the above to work
Update: added -v flag based on Jilles van Gurp response.
/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.
Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/
At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.
Update: Understanding Java From Command Line on OSX has a better explanation on how this works.
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
is the way to do it. Note, updating this to 1.8 works just fine.
For me, Mountain Lion 10.8.2, the solution most voted does not work.
I installed jdk 1.7 via Oracle and maven from homebrew.
My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too.
I add the below sentence in ~/.bash_profile, and it works.
export JAVA_HOME="$(/usr/libexec/java_home)"
This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.
None of the above answers helped me. I suppose all the answers are for older OS X
For OS X Yosemite 10.10, follow these steps
Use your favorite text editor to open: ~/.bash_profile
//This command will open the file using vim
$ vim ~/.bash_profile
Add the following line in the file and save it ( : followed by "x" for vim):
export JAVA_HOME=$(/usr/libexec/java_home)
Then in the terminal type the following two commands to see output:
$ source ~/.bash_profile
$ echo $JAVA_HOME
In the second line, you are updating the contents of .bash_profile file.
Update for Java 9 and some neat aliases.
In .bash_profile:
export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`
Note, that for the latest version it is 9 and not 1.9.
Set active Java:
export JAVA_HOME=$JAVA_HOME8
export PATH=$JAVA_HOME/bin:$PATH
Some additional alias to switch between the different versions:
alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'
Test in terminal:
% j8
% java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
% j9
% java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
EDIT: Update for Java 10
export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'
EDIT: Update for Java 11
export JAVA_HOME11=`/usr/libexec/java_home --version 11`
alias j11='export JAVA_HOME=$JAVA_HOME11; export PATH=$JAVA_HOME/bin:$PATH'
The above didn't work for me with Amazon's EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.
For OS X you can do:
export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
On Mac OS X Lion, to set visualgc to run, I used:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
The following worked for me. I'm using ZSH on OSX Yosemite with Java 8 installed.
The following command /usr/libexec/java_home emits the path to JDK home:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
In your ~/.zshrc,
export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"
for macOS Mojave 10.14.1 and JAVA 11.0.1
I set the profile as
export JAVA_HOME=$(/usr/libexec/java_home)
key in terminal this to confirm:
$JAVA_HOME/bin/java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
A better (more upgradable) way is to use the following:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
This should work with AWS also since it has bin underneath Home
Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
My approach is:
.bashrc
export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=$JAVA6_HOME
# -- optional
# export PATH=$JAVA_HOME/bin:$PATH
This makes it very easy to switch between J6 and J7
I Had to explicitly set it to the exact path on my Macbook air .
Steps followed:
try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
Edit the Bash p with - sudo nano ~/.bash_profile
Add the exact path to JAVA Home (with the path from step 2 above)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
Save and exit
Check JAVA_Home using - echo $JAVA_HOME
I am running MACOS MOJAVE - 10.14.2 (18C54) on a Macbook Air with JAVA 8
OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:
$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin
and in ~/.zshrc change JAVA_HOME to
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"
For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.
I am sure there is a better way but this got me up and running.
hughsmac:~ hbrien$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
For Java 11 (JDK 11) it can be located with the following command:
/usr/libexec/java_home -v 11
Got the same issue after I upgrade my Mac OS and following worked for me:
cmd>vi ~/.bash_profile
Add/update the line for JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home"
cmd>source ~/.bash_profile or open a new terminal
I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/
If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.
What you do is to edit your ~/.bash_profile and add the following:
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.7
What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using
setjdk 1.7
which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute
setjdk 1.7.0_51
instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.
P.S. Do not forget to source ~/.bash_profile after you save it.
For Fish terminal users on Mac (I believe it's available on Linux as well), this should work:
set -Ux JAVA_8 (/usr/libexec/java_home --version 1.8)
set -Ux JAVA_12 (/usr/libexec/java_home --version 12)
set -Ux JAVA_HOME $JAVA_8 //or whichever version you want as default
This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.
So, I set JAVA_HOME=/usr in my .bashrc.
Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.
Update
As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.
for mac user .
java 8 should add
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
java 6 :
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297
For Mac Yosemite,
JDK 1.7.0_xx is using
$ ls -ltar /usr/bin/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
Anyone using AUSKEY from the Australian Tax Office (ATO) should uninstall AUSKEY.
This sorted out my JAVA_HOME issues.
It is also no longer required for MAC users. Yah!
I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.
It's not automatically set up so to set it correctly on my system I run:
set -U JAVA_HOME (/usr/libexec/java_home)
Just set java_home of 1.8 jdk version in netbeans.conf file:
/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf
uncomment line:
netbeans_jdkhome="path/to/jdk"
and set path to your 1.8 jdk, in my case:
netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"
This approach lays you to have several jdk versions on mac os
Handy command for knowing java home and other details.
java -XshowSettings:properties -version
and to know all java homes on your mac:
/usr/libexec/java_home -V

Categories

Resources