JAVA_HOME directory in Linux

311,275

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.

Share:
311,275

Related videos on Youtube

Progress Programmer
Author by

Progress Programmer

evolving...

Updated on February 07, 2022

Comments

  • Progress Programmer
    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
    bulltorious almost 12 years
    Had to use sudo find / -name *javac to find the oracle VM I had installed
  • Matthew O'Riordan
    Matthew O'Riordan almost 10 years
    This 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
    JVE999 over 9 years
    You might want to try sudo find /usr/ -name *jdk* as my path was /usr/lib/jvm/java-7-openjdk-amd64/.
  • Peter Quiring
    Peter Quiring over 9 years
    JAVA_HOME is no longer defined in Linux systems and is deprecated.
  • harschware
    harschware about 9 years
    @PeterQuiring you should cite your source for that statement.. you are quite wrong.
  • Peter Quiring
    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
    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
    Peter Quiring almost 9 years
    @harschware It could be just an Ubuntu thing. Maybe other distros still define it.
  • jsdevel
    jsdevel over 8 years
    Thanks @MatthewO'Riordan! I added the -f flag for the linux command. Note that readlink on mac is not the GNU version so I excluded -f for the mac version of the script. Mac users that have the GNU version of readlink installed via brew can use -f.
  • gilbertpilz
    gilbertpilz over 8 years
    This is particularly nice for people who use /etc/alternatives to manage different java versions.
  • minhas23
    minhas23 over 8 years
    Worked fine on Mac OS
  • GKFX
    GKFX almost 8 years
    Unsafe 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
    cyberoblivion over 7 years
    Fantastic 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
    drwatsoncode about 7 years
    I'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
    Parth Mehrotra about 7 years
    Glad it helped!
  • ikaerom
    ikaerom about 7 years
    It'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
    ikaerom about 7 years
    A 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
    ikaerom about 7 years
    On OSX specifically, the following is preferred: /usr/libexec/java_home -v1.8 for example to display JDK 1.8's home.
  • ikaerom
    ikaerom about 7 years
    If 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
    Harvey Lin about 6 years
    Holy moly this one works better than all the others, thanks!
  • dr_
    dr_ almost 6 years
    This is the better answer.
  • Martin Majewski
    Martin Majewski over 5 years
    This is the best solution to find any home paths. I use it frequently for java, maven, and whatnot! Thank you!
  • soloturn
    soloturn about 4 years
    JAVA_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
    Andy over 3 years
    Maven installation documentation step 1 says "Ensure JAVA_HOME environment variable is set"
  • 6005
    6005 over 2 years
    I get this: "-bash: /usr/lib/jvm/java-8-openjdk-amd64: Is a directory"
  • 6005
    6005 over 2 years
    Ah, nevermind, the intention is clearer with an echo before it: echo $(dirname $(dirname $(readlink -f $(which javac))))