JAVA_HOME directory in Linux

JavaLinuxDirectory

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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 2 - Java

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 3 - Java

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 - Java

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 - Java

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.

Solution 6 - Java

If $JAVA_HOME is defined in your environment...

$ echo $JAVA_HOME
$ # I am not lucky...

You can guess it from the classes that are loaded.

$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]

This method ensures you find the correct jdk/jre used in case there are multiple installations.

Or using strace:

$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)

Solution 7 - Java

On the Terminal, type:

echo "$JAVA_HOME"

If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.

Solution 8 - Java

Did you set your JAVA_HOME

  • Korn and bash shells:export JAVA_HOME=jdk-install-dir
  • Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
  • C shell:setenv JAVA_HOME jdk-install-dir

Solution 9 - Java

Here's an improvement, grabbing just the directory to stdout:

java -XshowSettings:properties -version 2>&1 \
   | sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'

Solution 10 - Java

You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:

There are 3 programs which provide 'java'.

Selection Command

1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java) 2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java) *+ 3 /usr/java/jdk-17.0.2/bin/java

Enter to keep the current selection[+], or type selection number:

from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2

Solution 11 - Java

http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment

If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionProgress ProgrammerView Question on Stackoverflow
Solution 1 - JavajsdevelView Answer on Stackoverflow
Solution 2 - JavaAdamCView Answer on Stackoverflow
Solution 3 - JavaParth MehrotraView Answer on Stackoverflow
Solution 4 - JavacevarisView Answer on Stackoverflow
Solution 5 - JavaPaulo FidalgoView Answer on Stackoverflow
Solution 6 - JavadnozayView Answer on Stackoverflow
Solution 7 - JavaMichael Aaron SafyanView Answer on Stackoverflow
Solution 8 - JavaNizar GriraView Answer on Stackoverflow
Solution 9 - Javauser246672View Answer on Stackoverflow
Solution 10 - JavaJosephNgugiMuiruriView Answer on Stackoverflow
Solution 11 - JavaDouglas G. AllenView Answer on Stackoverflow