JAVA_HOME directory in Linux
Solution 1
echo $JAVA_HOME
will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.
If you try which java
and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.
Solution 2
On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))
On Mac you can run $(dirname $(readlink $(which javac)))/java_home
I'm not sure about windows but I imagine where javac
would get you pretty close
Solution 3
Just another solution, this one's cross platform (uses java
), and points you to the location of the jre.
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
Outputs all of java
's current settings, and finds the one called java.home
.
For windows, you can go with findstr instead of grep.
java -XshowSettings:properties -version 2>&1 | findstr "java.home"
Solution 4
I know this is late, but this command searches the /usr/ directory to find java for you
sudo find /usr/ -name *jdk
Results to
/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk
FYI, if you are on a Mac, currently JAVA_HOME is located at
/System/Library/Frameworks/JavaVM.framework/Home
Solution 5
To show the value of an environment variable you use:
echo $VARIABLE
so in your case will be:
echo $JAVA_HOME
In case you don't have it setted, you can add in your .bashrc
file:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and it will dynamically change when you update your packages.
Related videos on Youtube
Comments
-
Progress Programmer over 2 years
Is there any linux command I could use to find out
JAVA_HOME
directory? I've tried print out the environment variables ("env") but I can't find the directory. -
bulltorious almost 12 yearsHad to use sudo find / -name *javac to find the oracle VM I had installed
-
Matthew O'Riordan almost 10 yearsThis is a great solution, but remember to use the canonical flag for readlink as Java can be set up with multiple links, so use
$(dirname $(dirname $(readlink -f $(which javac))))
on Linux instead -
JVE999 over 9 yearsYou might want to try
sudo find /usr/ -name *jdk*
as my path was/usr/lib/jvm/java-7-openjdk-amd64/
. -
Peter Quiring over 9 yearsJAVA_HOME is no longer defined in Linux systems and is deprecated.
-
harschware about 9 years@PeterQuiring you should cite your source for that statement.. you are quite wrong.
-
Peter Quiring about 9 years@harschware When I install Ubuntu the JAVA_HOME is not set, even with the JDK installed. Also read /usr/share/doc/openjdk-8-jre-headless/JAVA_HOME and it says the JAVA_HOME should no longer be needed (legacy).
-
harschware about 9 years@PeterQuiring Thanks Peter, unfortunately I don't have easy access to that file. Perhaps there is an effort to deprecate it in Java 8, but the shear number of tools that rely on JAVA_HOME will mandate a need to continue to define the variable for many years yet...
-
Peter Quiring almost 9 years@harschware It could be just an Ubuntu thing. Maybe other distros still define it.
-
jsdevel over 8 yearsThanks @MatthewO'Riordan! I added the
-f
flag for the linux command. Note thatreadlink
on mac is not theGNU
version so I excluded-f
for the mac version of the script. Mac users that have theGNU
version ofreadlink
installed via brew can use-f
. -
gilbertpilz over 8 yearsThis is particularly nice for people who use /etc/alternatives to manage different java versions.
-
minhas23 over 8 yearsWorked fine on Mac OS
-
GKFX almost 8 yearsUnsafe sed command: what if /usr/bin/java was a symlink to
/usr/bin/java-8/bin/java
? You'd get the output/usr/-8/bin/java
. -
cyberoblivion over 7 yearsFantastic Solution! if you want to use jdk as home instead of jre just tweak readlink -f /usr/bin/java | sed "s:jre/bin/java::"
-
drwatsoncode about 7 yearsI've reseached a lot of ways to find JAVA_HOME and I think this is one of the best! Thanks for mentioning it!
-
Parth Mehrotra about 7 yearsGlad it helped!
-
ikaerom about 7 yearsIt's probably worth mentioning that this started working with JDK1.7 (IIRC). Before that you would have needed to call:
jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));'
. In both cases, for 'cross-platform' aspects, it is recommendable to prepend a/usr/bin/env
. -
ikaerom about 7 yearsA bit shorter, though with three forks instead of two, is the following:
: ${JAVA_HOME:=$(/usr/bin/env java -XshowSettings:properties -version 2>&1 | grep "java.home" | cut -d"=" -f2)}
-
ikaerom about 7 yearsOn OSX specifically, the following is preferred:
/usr/libexec/java_home -v1.8
for example to display JDK 1.8's home. -
ikaerom about 7 yearsIf one needs to export the path directly:
export $(/usr/bin/env java -XshowSettings:properties -version 2>&1 | grep "java.home" | sed -e 's/java.home/JAVA_HOME/;s/ //g;')
-
Harvey Lin about 6 yearsHoly moly this one works better than all the others, thanks!
-
dr_ almost 6 yearsThis is the better answer.
-
Martin Majewski over 5 yearsThis is the best solution to find any home paths. I use it frequently for java, maven, and whatnot! Thank you!
-
soloturn about 4 yearsJAVA_HOME should not be set, java knows where it is installed. if you need to find out you might use: java -XshowSettings:properties -version 2>&1 | grep "java.home" as Parth suggested below.
-
Andy over 3 yearsMaven installation documentation step 1 says "Ensure JAVA_HOME environment variable is set"
-
6005 over 2 yearsI get this: "-bash: /usr/lib/jvm/java-8-openjdk-amd64: Is a directory"
-
6005 over 2 yearsAh, nevermind, the intention is clearer with an echo before it:
echo $(dirname $(dirname $(readlink -f $(which javac))))