JAVA_HOME directory in Linux
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.
11 Answers 11
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
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
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 .
This is the best solution to find any home paths. I use it frequently for java, maven, and whatnot! Thank you!
Ah, nevermind, the intention is clearer with an echo before it: echo $(dirname $(dirname $(readlink -f $(which javac))))
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"
I’ve reseached a lot of ways to find JAVA_HOME and I think this is one of the best! Thanks for mentioning it!
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 .
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.
@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).
@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.
I know this is late, but this command searches the /usr/ directory to find java for you
/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
You might want to try sudo find /usr/ -name *jdk* as my path was /usr/lib/jvm/java-7-openjdk-amd64/ .
To show the value of an environment variable you use:
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.
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 .
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::»
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.
$ 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)
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.
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
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:]]*//'
A bit shorter, though with three forks instead of two, is the following: : $
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;’)
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
How to find my current JAVA_HOME in ubuntu?
To display JAVA_HOME variable path, type in terminal:
If nothing appears then set it with this:
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
This will differ according to your JDK type and version.
For displaying it again, follow the first command.
Follow JREs from different vendors on the same system, for using different JDK’s or switch between JDK’s.
It gives «/usr/lib/jvm/java-6-sun». But I have installed java 7. When I check it using «java -version» it gives java version «1.7.0_45» Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) Server VM (build 24.45-b08, mixed mode)
then execute second command for setting JAVA_HOME variable. NOTE: JAVA_HOME doesn’t make jdk default, it just makes JAVA_HOME variable set to a path & if you want to use different jdk installed on same machine then check my answer, I have edited it.
@Jax-L But now when I give echo JAVA_HOME it just displays as «JAVA_HOME». The path I gave is not displaying.
export works only until you restart. Or you add export to the .bashrc login script. But the correct way to set such environment variables is in /etc/environment
If you have JDK 1.6 (corresponding to Java 6) or a newer version installed, you should have a program named jrunscript in your PATH . You can use this to find the corresponding JAVA_HOME . Example:
$ jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));' /opt/local/jdk1.7.0_76/jre
You could set the environment variable like this:
$ export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"
Note that the JRE doesn’t include jrunscript , so this will only work if you install the JDK, not just the JRE.
Discover from a batch file where is Java installed?
This snippet will search the current PATH for java.exe, and print out where it was found:
for /f %%j in ("java.exe") do @echo.%%~dp$PATH:j
On my system this gives me
Using this you can set JAVA_HOME as follows:
@echo off for /f %%j in ("java.exe") do ( set JAVA_HOME=%%~dp$PATH:j ) if %JAVA_HOME%.==. ( @echo java.exe not found ) else ( @echo JAVA_HOME = %JAVA_HOME% )
system32 is not where the JRE will be installed. See mindprod.com/jgloss/registry.html#JAVAFIND JAVA_HOME is typically used in scripts in this form to launch apps: %JAVA_HOME%\bin\java
I’m not saying it is, but that’s where it happens to reside in my path. From another answer (or comments to another answer, I don’t know, it’s been deleted) you may have an installer that copies java.exe to the %systemroot%\system32 directory. On my system, it apparently has.
@mishal153, yes, this is checking for empty strings. BAT files treat whitespace oddly, so there’s no easy way to check for empty strings. By putting a period at the end of a string and comparing to a literal that’s just a period, you can tell if the string is empty.
@PatrickCuff — Nice solution! JAVA_HOME is the parent directory of the directory that contains java.exe though, so you should traverse one directory up from the result found. java.exe is expected to be found at %JAVA_HOME%\bin\java.exe
This solution depends on the JDK being installed under %ProgramFiles%\Java, for example C:\Program Files\Java\jdk1.6.0_18. You can change the line «set JDK_Version=1.6» to the version you want to use such as «set JDK_Version=1.5».
Assuming that the latest version of JDK would be at the bottom of the list (jdk%jdk_Version%*) the latest version available should be set as JAVA_HOME. If the JDK could not be found JAVA_HOME will not be changed. If the JDK could not be found and JAVA_HOME doesn’t have a value the script will display an error message.
@echo off rem set the version of jdk you would like to use (1.4, 1.5, 1.6, etc) set JDK_Version=1.6 echo. echo Locating JDK %JDK_Version% for /d %%i in ("%ProgramFiles%\Java\jdk%jdk_Version%*") do (set Located=%%i) rem check if JDK was located if "%Located%"=="" goto else rem if JDK located display message to user rem update %JAVA_HOME% set JAVA_HOME=%Located% echo Located JDK %jdk_Version% echo JAVA_HOME has been set to: echo %JAVA_HOME% goto endif :else rem if JDK was not located rem if %JAVA_HOME% has been defined then use the existing value echo Could not locate JDK %JDK_Version% if "%JAVA_HOME%"=="" goto NoExistingJavaHome echo Existing value of JAVA_HOME will be used: echo %JAVA_HOME% goto endif :NoExistingJavaHome rem display message to the user that %JAVA_HOME% is not available echo No Existing value of JAVA_HOME is available goto endif :endif rem clear the variables used by this script set JDK_Version= set Located=