398

I need to know where JDK is located on my machine.

On running Java -version in cmd, it shows the version as '1.6.xx'. To find the location of this SDK on my machine I tried using echo %JAVA_HOME% but it is only showing 'JAVA_HOME' (as there is no 'JAVA_PATH' var set in my environment variables).

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ashine
  • 4,517
  • 3
  • 24
  • 26
  • 2
    unfortunately, I think some of the answers below may not work because apparently something changed with more recent versions of Java. Today I downloaded the version 7.60 Java SDK. After some searching, found it at C:\Program Files (x86)\Java\jdk1.7.0_60. There was an older version 6 JRE already installed. It was at C:\Program Files\Java and this was on the %PATH% not the newer install. – likejudo Jun 10 '14 at 16:41
  • 1
    The best answer is at the bottom, a one-liner from majkinetor for PowerShell: $p='HKLM:\SOFTWARE\JavaSoft\Java Development Kit'; $v=(gp $p).CurrentVersion; (gp $p/$v).JavaHome – JohnP2 Mar 14 '18 at 22:51
  • 1
    one more thing, if your jdk was bundled with intellij idea, it could be somewhere near the idea installation. For me it was in a folder 1 directory up from the idea folder, and the PATH variable pointed to a jre in Program Files – KGS Oct 16 '18 at 18:38

25 Answers25

450

If you are using Linux/Unix/Mac OS X:

Try this:

$ which java

Should output the exact location.

After that, you can set JAVA_HOME environment variable yourself.

In my computer (Mac OS X - Snow Leopard):

$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 Nov  7 07:59 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

If you are using Windows:

c:\> for %i in (java.exe) do @echo.   %~$PATH:i
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
150

Windows > Start > cmd >

C:> for %i in (javac.exe) do @echo.   %~$PATH:i

If you have a JDK installed, the Path is displayed,
for example: C:\Program Files\Java\jdk1.6.0_30\bin\javac.exe

grokster
  • 5,919
  • 1
  • 36
  • 22
  • 16
    Windows(7+) => where javac.exe – hB0 Nov 05 '13 at 17:20
  • 6
    In windows it is, where java – Pabitra Dash Jul 01 '15 at 12:39
  • 13
    `where java` works only if the executable is in the PATH. If for whatever reason, javac is not in the path, it won't return any result, but it doesn't mean JDK is not installed. So in general case, the file search solution proposed by @grokster solution will guarantee a result (thought might take time to complete). – Nikita R. Jul 15 '15 at 21:55
  • 1
    The 'for-loop' stuff did not work, however, I found it here: C:\Program Files (x86)\Java\jdk1.6.0_24\bin> – AjayKumarBasuthkar Jul 11 '16 at 13:06
  • Notice that if you are following this (https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#0) tutorial to use android studio, the actual path would not be what is returned by the command above. Most likely, you would have something like "C:\\Program Files\Java|" where instead of "jdk" you would have something like "jdk-10.0.1" for JDK version 10. – Euler_Salter Jul 19 '18 at 13:42
135

In Windows at the command prompt

where javac

NanoBennett
  • 1,802
  • 1
  • 13
  • 13
42

Command line:

Run where java on Command Prompt.

enter image description here

GUI:

On Windows 10 you can find out the path by going to Control Panel > Programs > Java. In the panel that shows up, you can find the path as demonstrated in the screenshot below. In the Java Control Panel, go to the 'Java' tab and then click the 'View' button under the description 'View and manage Java Runtime versions and settings for Java applications and applets.'

This should work on Windows 7 and possibly other recent versions of Windows.

enter image description here

smartexpert
  • 2,625
  • 3
  • 24
  • 41
34

In windows the default is: C:\Program Files\Java\jdk1.6.0_14 (where the numbers may differ, as they're the version).

Ronen Rabinovici
  • 8,680
  • 5
  • 34
  • 46
  • how would you retrieve the differing version number? – LZH Apr 04 '15 at 00:18
  • just navigate to the location incrementally from your explorer, and you'll see the jdk folder version that you installed. – ahnbizcad May 04 '15 at 19:33
  • Sometimes the JDK is distributed from 3rd parties, in which case you may not have Java under Program FIles, @Pablo answer helped locate perfectly – Naga Jul 23 '22 at 15:31
27

Plain and simple on Windows platforms:

where java

luccaa
  • 297
  • 3
  • 2
  • 9
    It results in wrong Java location C:\WINDOWS\System32\java.exe. But the actual jdk is installed at C:\Java\jdk1.7.0\ (as JAVA_HOME is set) – Himanshu Bhardwaj Dec 26 '14 at 14:02
27

Java installer puts several files into %WinDir%\System32 folder (java.exe, javaws.exe and some others). When you type java.exe in command line or create process without full path, Windows runs these as last resort if they are missing in %PATH% folders.

You can lookup all versions of Java installed in registry. Take a look at HKLM\SOFTWARE\JavaSoft\Java Runtime Environment and HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment for 32-bit java on 64 bit Windows.

This is how java itself finds out different versions installed. And this is why both 32-bit and 64-bit version can co-exist and works fine without interfering.

Denis The Menace
  • 505
  • 4
  • 10
21

Under Windows, you can use

C:>dir /b /s java.exe

to print the full path of each and every "java.exe" on your C: drive, regardless of whether they are on your PATH environment variable.

Thomas Bender
  • 211
  • 2
  • 2
19

The batch script below will print out the existing default JRE. It can be easily modified to find the JDK version installed by replacing the Java Runtime Environment with Java Development Kit.

@echo off

setlocal

::- Get the Java Version
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% 2>nul || (
    echo JRE not installed 
    exit /b 1
)
set JRE_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JRE_VERSION=%%b
)

echo JRE VERSION: %JRE_VERSION%

::- Get the JavaHome
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%JRE_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% 2>nul || (
    echo JavaHome not installed
    exit /b 1
)

set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JAVAHOME=%%b
)

echo JavaHome: %JAVAHOME%

endlocal
munsingh
  • 317
  • 2
  • 9
15

In a Windows command prompt, just type:

set java_home

Or, if you don't like the command environment, you can check it from:

Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and in system variable try to find JAVA_HOME.

enter image description here

Husam
  • 13
  • 2
Johann
  • 27,536
  • 39
  • 165
  • 279
  • This gives me the jdk folder. When I run System.getProperty("java.home") in a Scala Worksheet, it gives me the jre subfolder. There's apparently some ambiguity about Java Home. – Ion Freeman May 10 '17 at 18:34
15

In Windows PowerShell you can use the Get-Command function to see where Java is installed:

Get-Command -All java

Or

gcm -All java

The -All part makes sure to show all places it appears in the Path lookup. Below is example output.

PS C:> gcm -All java

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     java.exe                                           8.0.202.8  C:\Program Files (x86)\Common Files\Oracle\Java\jav...
Application     java.exe                                           8.0.131... C:\ProgramData\Oracle\Java\javapath\java.exe
Scott H
  • 2,644
  • 1
  • 24
  • 28
12

Powershell one liner:

$p='HKLM:\SOFTWARE\JavaSoft\Java Development Kit'; $v=(gp $p).CurrentVersion; (gp $p/$v).JavaHome
majkinetor
  • 8,730
  • 9
  • 54
  • 72
10

More on Windows... variable java.home is not always the same location as the binary that is run.

As Denis The Menace says, the installer puts Java files into Program Files, but also java.exe into System32. With nothing Java related on the path java -version can still work. However when PeterMmm's program is run it reports the value of Program Files as java.home, this is not wrong (Java is installed there) but the actual binary being run is located in System32.

One way to hunt down the location of the java.exe binary, add the following line to PeterMmm's code to keep the program running a while longer:

try{Thread.sleep(60000);}catch(Exception e) {}

Compile and run it, then hunt down the location of the java.exe image. E.g. in Windows 7 open the task manager, find the java.exe entry, right click and select 'open file location', this opens the exact location of the Java binary. In this case it would be System32.

Moika Turns
  • 677
  • 11
  • 17
10

Run this program from commandline:

// File: Main.java
public class Main {

    public static void main(String[] args) {
       System.out.println(System.getProperty("java.home"));
    }

}


$ javac Main.java
$ java Main
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
8

Have you tried looking at your %PATH% variable. That's what Windows uses to find any executable.

sblundy
  • 60,628
  • 22
  • 121
  • 123
6

Just execute the set command in your command line. Then you see all the environments variables you have set.

Or if on Unix you can simplify it:

$ set | grep "JAVA_HOME" 
  • Thanks Roflcoptr, it was indeed useful to learn about 'set'. In my system its is showing two entries for same variable, how can I delete the incorrect one. – Ashine Jan 13 '11 at 16:20
4

This is OS specific. On Unix:

which java

will display the path to the executable. I don't know of a Windows equivalent, but there you typically have the bin folder of the JDK installation in the system PATH:

echo %PATH%
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
4

On macOS, run:

cd /tmp && echo 'public class Main {public static void main(String[] args) {System.out.println(System.getProperty("java.home"));}}' > Main.java && javac Main.java && java Main

On my machine, this prints:

/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home

Note that running which java does not show the JDK location, because the java command is instead part of JavaVM.framework, which wraps the real JDK:

$ which java
/usr/bin/java
/private/tmp
$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 14 Nov 17:37 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
2

None of these answers are correct for Linux if you are looking for the home that includes the subdirs such as: bin, docs, include, jre, lib, etc.

On Ubuntu for openjdk1.8.0, this is in: /usr/lib/jvm/java-1.8.0-openjdk-amd64

and you may prefer to use that for JAVA_HOME since you will be able to find headers if you build JNI source files. While it's true which java will provide the binary path, it is not the true JDK home.

EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
1

I have improved munsingh's answer above by testing for the registry key in 64-bit and 32-bit registries, if needed:

::- Test for the registry location  
SET VALUE=CurrentVersion
SET KEY_1="HKLM\SOFTWARE\JavaSoft\Java Development Kit"
SET KEY_2=HKLM\SOFTWARE\JavaSoft\JDK
SET REG_1=reg.exe
SET REG_2="C:\Windows\sysnative\reg.exe"
SET REG_3="C:\Windows\syswow64\reg.exe"

SET KEY=%KEY_1%
SET REG=%REG_1%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_1%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

::- %REG_2% is for 64-bit installations, using "C:\Windows\sysnative"
SET KEY=%KEY_1%
SET REG=%REG_2%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_2%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

::- %REG_3% is for 32-bit installations on a 64-bit system, using "C:\Windows\syswow64"
SET KEY=%KEY_1%
SET REG=%REG_3%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

SET KEY=%KEY_2%
SET REG=%REG_3%
%REG% QUERY %KEY% /v %VALUE% 2>nul
IF %ERRORLEVEL% EQU 0 GOTO _set_value

:_set_value
FOR /F "tokens=2,*" %%a IN ('%REG% QUERY %KEY% /v %VALUE%') DO (
    SET JDK_VERSION=%%b
)
SET KEY=%KEY%\%JDK_VERSION%
SET VALUE=JavaHome
FOR /F "tokens=2,*" %%a IN ('%REG% QUERY %KEY% /v %VALUE%') DO (
    SET JAVAHOME=%%b
)
ECHO "%JAVAHOME%"
::- SETX JAVA_HOME "%JAVAHOME%"

reference for access to the 64-bit registry

JohnP2
  • 1,899
  • 19
  • 17
1

Maybe the above methods work... I tried some and didn't for me. What did was this :

Run this in terminal :

/usr/libexec/java_home
Mudit Verma
  • 374
  • 2
  • 6
1

Simple method (Windows): Open an application using java. press ctrl + shift + esc

Right click on OpenJDK platform binary. Click open file location. Then it will show java/javaw.exe then go to the top where it shows the folder and click on the jdk then right copy the path, boom. (Wont work for apps using bundled jre paths/runtimes, because it will show path to the bundled runtime)

Dharman
  • 30,962
  • 25
  • 85
  • 135
deateaterOG
  • 111
  • 1
  • 3
  • 9
0
#!/bin/bash

if [[ $(which ${JAVA_HOME}/bin/java) ]]; then
    exe="${JAVA_HOME}/bin/java"
elif [[ $(which java) ]]; then
    exe="java"
else 
    echo "Java environment is not detected."
    exit 1
fi

${exe} -version

For windows:

@echo off
if "%JAVA_HOME%" == "" goto nojavahome

echo Using JAVA_HOME            :   %JAVA_HOME%

"%JAVA_HOME%/bin/java.exe" -version
goto exit

:nojavahome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program.
goto exit

:exit

This link might help to explain how to find java executable from bash: http://srcode.org/2014/05/07/detect-java-executable/

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Shaiful Islam Jun 10 '15 at 16:14
  • 1
    Thanks @ShaifulIslam, your point is right. I added the code from the link. –  Jun 10 '15 at 17:14
0

in Windows cmd:

set "JAVA_HOME" 
Husam
  • 13
  • 2
0

Script for 32/64 bit Windows.

@echo off

setlocal enabledelayedexpansion

::- Get the Java Version
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
set KEY64="HKLM\SOFTWARE\WOW6432Node\JavaSoft\Java Runtime Environment"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% 2>nul || (
    set KEY=!KEY64!
    reg query !KEY! /v %VALUE% 2>nul || (
    echo JRE not installed 
    exit /b 1
)
)

set JRE_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JRE_VERSION=%%b
)

echo JRE VERSION: %JRE_VERSION%

::- Get the JavaHome
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%JRE_VERSION%"
set KEY64="HKLM\SOFTWARE\WOW6432Node\JavaSoft\Java Runtime Environment\%JRE_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% 2>nul || (
    set KEY=!KEY64!
    reg query !KEY! /v %VALUE% 2>nul || (
    echo JavaHome not installed
    exit /b 1
)
)

set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
    set JAVAHOME=%%b
)

echo JavaHome: %JAVAHOME%

endlocal
Rudixx
  • 105
  • 1
  • 1
  • 7