237

When trying to check the current version of Java in which I am running, I received the error "java is not recognized as an internal or external command, operable program or batch file.".

I am running Windows 7 OS and have downloaded the latest JDK and felt I may have accidentally deleted the java from machine as before I was able to check the Java version using the command "java -version".

What software must I download to get Java working on my machine again?

EDIT:

I have managed to get Java running from my cmd again after ensuring all environment variables pointed to the current Java SDK.

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Karen
  • 2,469
  • 3
  • 14
  • 10

19 Answers19

283

You need to configure your environment variables, JAVA_HOME and PATH.

JAVA_HOME must contain the path to java, and you should add %JAVA_HOME%\bin to PATH

Alternatively, you can simply add to your PATH the whole path to the bin folder, without the JAVA_HOME variable, however, this makes a little more annoying when you need to have more than one java version on your machine (that way you only need to change JAVA_HOME and don't even bother with PATH)

fcm
  • 6,305
  • 5
  • 24
  • 37
  • in my case i had jdk 7 installed and it worked just fine until i updated to newer jdk 8 version, the solution was to uninstall the old version of jdk – moein rahimi Nov 07 '17 at 06:01
  • 1
    It works for me after delete the JAVA_HOME and set the whole path to the java bin folder into the first of the Path variable – Mohammad Heydari May 11 '18 at 01:35
  • I already got a `PATH` variable. Can I change the content without ruining anything else? – Liggliluff Oct 21 '18 at 08:39
  • remember to add BOTH variables "JAVA_HOME" and then "PATH", both on upper case – Marco Ottina Jan 08 '20 at 22:00
  • 3
    For some reason in Windows 10, this won't work. Put the full path in `PATH` variable instead of using `%JAVA_HOME%`. Check out this answer: https://stackoverflow.com/a/62590863/3337089 – Nagabhushan S N Jun 26 '20 at 09:43
  • @NagabhushanSN `%JAVA_HOME%` just worked for me on Windows 10 using GUI: https://i.imgur.com/Z9EoqCg.png . Before I added the environment variable, I'd get `java is not recognized as an internal or external command` error when I entered `java -version` into the console. – KulaGGin Mar 11 '22 at 14:06
  • Notice: Change the PATH variable in the "System Variables" and not the "User Variables". – Ariel Rotem Aug 05 '22 at 13:27
  • I was trying to install Open JDK 11 without having any JDK already installed in the system. %JAVA_HOME% did not work for me in Windows 8. I deleted the JAVA_HOME variable and gave whole path to java bin folder in Path variable and then it worked. – Mayur Patel Mar 22 '23 at 05:49
  • I had already JAVA_HOME but setting path with bin resolved – Shahid Hussain Abbasi May 18 '23 at 07:33
  • 1
    The relevance of the `JAVA_HOME` variable is a tenacious myth. Java 1.0 and 1.1 used this variable but since then (for a quarter century now), this variable is not used by Java at all. Only some 3rd party tools use this variable as a convention to find the Java installation. – Holger Jun 06 '23 at 07:58
204

For Windows 7:

  1. Right click on My Computer

  2. Select Properties

  3. Select Advanced System Settings

  4. Select the Advanced tab

  5. Select Environment Variables

  6. Select Path under System Variables

  7. Click on the Edit button

  8. In Variable value editor paste this at the start of the line

    C:\Program Files\Java\jdk1.7.0_72\bin;
    
  9. Click Ok then Ok again

  10. Restart command prompt otherwise it won't see the change to the path variable

  11. Type java -version in the command prompt.


Notes on Step 8:

  1. The version of java in this may be different from the one used here -- this is only an example.
  2. There will probably be other values in the path variable. It is really important that you don't delete what's already there. That's why the instructions say to paste the given value at the start of the line -- this means that you don't remove the existing value, you just put java before it. This also fixes any problems you'd be getting if an other version of java is also on the path.

Notes on Step 6:

  1. This sets the path for the computer, not for the individual user. It may be that you're working on a computer which other developers also use, in which case you'd rather set the user variables, rather than the system variables
Troll
  • 1,895
  • 3
  • 15
  • 34
Veeresh Hatti
  • 2,041
  • 1
  • 10
  • 2
64

It sounds like you haven't added the right directory to your path.

First find out which directory you've installed Java in. For example, on my box it's in C:\Program Files\java\jdk1.7.0_111. Once you've found it, try running it directly. For example:

c:\> "c:\Program Files\java\jdk1.7.0_11\bin\java" -version

Once you've definitely got the right version, add the bin directory to your PATH environment variable.

Note that you don't need a JAVA_HOME environment variable, and haven't for some time. Some tools may use it - and if you're using one of those, then sure, set it - but if you're just using (say) Eclipse and the command-line java/javac tools, you're fine without it.


1 Yes, this has reminded me that I need to update...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @SotiriosDelimanolis: I can't remember the last time I needed it, personally. *Some* tools may - but I'd only start changing the environment when I actually needed to, rather than just as a matter of course. (I'll edit my answer to indicate that.) – Jon Skeet Apr 03 '13 at 20:07
  • 1
    Thank you very much for your answer however after adding both the path to java in both the "Path" and "JAVA_HOME" environment variables I still seem to be receiving the same error. – Karen Apr 03 '13 at 20:33
  • 2
    @Karen: Well did you try my first step of running it explicitly? And have you restarted your command prompt after changing your environment variables? – Jon Skeet Apr 03 '13 at 20:35
  • When I ran _italic_ c:\> "c:\Program Files\java\jdk1.7.0_11\bin\java" -version _italic_ it said "java version '1.7.0_11' Java SE Runtime Environment Java HotSpot Client VM – Karen Apr 03 '13 at 20:37
  • Yes I have restarted the cmd after changing the environment variables but there seems to have been no change unfortunately. – Karen Apr 03 '13 at 20:39
  • @Karen: Are you sure you added the full bin directory to your path? What does your path entry look like? – Jon Skeet Apr 03 '13 at 20:40
  • @HockeyJ: You don't need `JAVA_HOME` in order to run Java itself successfully. What environment variables *other applications* use is a different matter. Hence "Some tools may use it" in my answer. – Jon Skeet Jan 14 '17 at 14:53
59

Assume, Java/JDK is installed to the folder: C:\Program Files\Java:

Java/JDK installation path

Follow the steps:

  1. Goto Control Panel → System → Advanced system settings → Advanced → Environment variables (Win+Pause/Break for System in Control Panel)
  2. In the System variables section click on New…
  3. In Variable name write: JAVA_HOME
  4. In Variable value write: C:\Program Files\Java\bin, press OK: Add JAVA_HOME
  5. In the System variables section double click on Path
  6. Press New and write C:\Program Files\Java\bin, press OK: Add Java Path
  7. In Environment variables window press OK
  8. Restart/Run cmd.exe and write: java --version: Java version CMD
Mike
  • 14,010
  • 29
  • 101
  • 161
Vijay Bhatt
  • 1,351
  • 13
  • 13
  • 2
    I just tried with window 10. The command `java --version` does not work but `java -version` works. – Steve Jun 30 '20 at 13:05
  • 1
    I am really late, but this is the first time I am having an issue with this, my JDK installation is on the C: drive, the project location is on the D:\ drive. I have updated the %JAVA_HOME% to point to JDK'S bin even though it has been there but not pointing directly to \bin, yet am still presented with the "java is not recognized " message – Riidonesh Mar 23 '21 at 09:14
26

Search environment variables. enter image description here

open the "edit the system environment variables". then click on "environment variables". enter image description here

Under "User variables" click on "Path" then "Edit". enter image description here

Find your Java path and click "Edit". enter image description here

then paste the path of your java installation folder. Mostly you can find it on a path similar to this. C:\Program Files\Java\jdk-12.0.2\bin

Then click OK. now in the start menu, type cmd. open the command prompt. type java -version If you did it right,it should show something like this. enter image description here

Johira Afzali
  • 1,196
  • 11
  • 10
10

For me its start working after putting ,: in the starting of the system variable path :--


enter image description here

enter image description here


Anurag_BEHS
  • 1,390
  • 2
  • 22
  • 36
7

My solution was to put same value (path to JDK bin folder) in JAVA_HOME and Path

JAVA_HOME Path

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
naru
  • 81
  • 1
  • 1
  • 1
    Welcome to SO. Please add some more information - e.g. what you actually put into `JAVA_HOME` and `PATH` and don't refer to other answers. – Michael Lihs Jan 24 '17 at 21:33
5

In my case, PATH was properly SET but PATHEXT has been cleared by me by mistake with .exe extension. That why window can't find java or anything .exe application from command prompt. Hope it can help someone.

Breton F.
  • 177
  • 1
  • 6
5

This problem is on Windows 8. First copy your Path of java jdk - e.g. C:\Program Files\Java\jdk1.7.0_51\bin.

  1. Right on the My Computer Icon on the Desktop and Click Properties.

  2. Select 'Advanced System Settings' in the left pane.

  3. Under 'Advanced' tab, select 'Environment Variables' at the bottom.

  4. In System Variables, select 'Path' Variable and edit it.

Paste the path and add a ';' at the end - e.g. C:\Program Files\Java\jdk1.7.0_51\bin;

Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
besartm
  • 558
  • 1
  • 7
  • 14
2

I had the same problem. Just Install the exact bit of java as of your computer. If your PC is 64 bit then install 64 bit java. If it is 32 bit then vice versa :)

Pratik
  • 21
  • 1
2

Not sure why, but in my case, the reason was because I was running Anaconda terminal instead of the CMD.

After I use CMD and update the path settings as mentioned by all comments above the issue solved on my side.

HassanSh__3571619
  • 1,859
  • 1
  • 19
  • 18
2

In case you are using a laptop and do not have the Pause\Break button. For windows 10 users with 20h2 and above:

1 WINtype "About your PC" → scroll at the bottom → Advanced system settings → Environment variables or WIN+Rshell:::{bb06c0e4-d293-4f75-8a90-cb05b6477eee} to open Classic System Properties → on the left side → Advanced system settings → Environment variables

Also for Windows 10 and Windows 7:

1 WIN → This PC → properties → Advanced system settings → Environment variables

  1. In the System variables section click on New…
  2. In Variable name write: JAVA_HOME
  3. In Variable value write: C:\Program Files\Java\jdk-15.0.2\bin, press OK

I have taken steps 2-4 from Vijay Bhatt

Adding a new system variable in UI

There is a bit faster way to set a system variable. Run a console (terminal) as an administrator.

General command synax to add a new variable:

setx variableName value /M

In our example, we would need to set it as

setx JAVA_HOME "C:\Program Files\Java\jdk-15.0.2\bin" /M`

Like this: Adding a new system variable using admin console

/M - flag specifies to set the variable in the system environment. After command execution, you should see the message: SUCCESS: Specified value was saved.

How can I check that it is added?

  1. Close your active terminal;
  2. Open your favorite terminal;
  3. Type java -version.

You should see something similar to this: Result image

Notice: User variables can be created w/o having a root (administrator privileges), whereas to create System variable; You need to open a console as a root.

I have used C:\Program Files\Java\jdk-15.0.2\bin as an example, in your case, it could be different from mine.

Utmost Creator
  • 814
  • 1
  • 9
  • 21
2

I opened a new command prompt in Windows 10 after updating the environment variables without closing the old one(To have my commands handy and lazy to type again) Still, the new cmd window was referring to the previous version of Java.

Then once I closed the all cmd prompts that ran with admin privileges, the new java version was getting reflected.

vsriram92
  • 593
  • 1
  • 4
  • 15
2

Restart the command prompt before checking the version of JDK installed. I spent 02 days on it until my problem resolved when I restarted the command prompt before checking javac - version, javac etc.

Rab Nawaz
  • 33
  • 4
1

I corrected my path variable but command prompt need to Restart otherwise, it won't be able to verify the change to the path variable. May be helpful for someone like me. so "restart command prompt"

Ganesh
  • 1,136
  • 13
  • 25
0

if you have cygwin installed in the Windows Box, or using UNIX Shell then

Issue bash#which java

This will tell you whether java is in your classpath or NOT.

anish
  • 6,884
  • 13
  • 74
  • 140
0

If you have set the environment variables (JAVA_HOME and PATH) under user variables, command prompt (run as administrator) will not identify java. For that you need to set environment variables under system variables.

0

Try this:

System variables: PATH = /bin folder of your jdk install

https://docs.oracle.com/javase/10/install/installation-jdk-and-jre-microsoft-windows-platforms.htm

NinelSeyer
  • 21
  • 1
  • 4
0

Just some extra information for people that have still problems, instead of editing the path variable of java, delete that part of the path(only the java!!) and make a new variable pointing tpo the jdk/jre.

this seemed to work for me.

cc2k
  • 351
  • 1
  • 2
  • 12