35

I have recently downloaded Maven and followed the instructions given on this this page. I already have ant installed on my machine. Now, if I want to verify that Maven is installed perfectly or not it is giving me error that JAVA_HOME is not set correctly, but same works perfectly fine for ANT.

For Maven I tried :
1. open cmd
2. type mvn -version
3. Error appeared :
C:\Users\Admin>mvn -version

ERROR: JAVA_HOME is set to an invalid directory.
JAVA_HOME = "C:\Program Files\Java\jre7\bin"
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation

For ANT I tried and worked :
1. open cmd
2. type mvn -version
3. Apache Ant(TM) version 1.9.1 compiled on May 15 2013

I went to the directory to check that java.exe is actually there in that directory or not and it was there. I checked the environment variables they set fine. I restarted the system and checked again but same problem. Please let me know what am I missing.

paul
  • 4,333
  • 16
  • 71
  • 144
  • 1
    try this.. `C:\ YourFolder >set path=C:\Program Files\Java\jdk1.7.0_09\bin;`, in this case, my jdk version is `1.7.0_09` **OR** you can looked out [this question](http://stackoverflow.com/questions/7709041/javac-is-not-recognized-as-an-internal-or-external-command-operable-program-or/16562626#16562626). Welcome to [StackOverFlow](http://stackoverflow.com/).. ^^ – Ahmad Azwar Anas Jun 26 '13 at 08:53
  • Set valid JAVA_HOME path in System environment – Sachin Jun 26 '13 at 08:47
  • 3
    With Maven, your JAVA_HOME must NOT CONTAIN the "bin", just ...\jdk1.7.0_09\ is enough. – Andiana Mar 02 '17 at 02:35
  • If you did everything correctly and it still doesn't work, try **restarting** your machine –  Mar 31 '20 at 13:14

11 Answers11

87

JAVA_HOME should point to jdk directory and not to jre directory. Also JAVA_HOME should point to the home jdk directory and not to jdk/bin directory.

Assuming that you have JDK installed in your program files directory then you need to set the JAVA_HOME like this:

JAVA_HOME="C:\Program Files\Java\jdkxxx"

xxx is the jdk version

Follow this link to learn more about setting JAVA_HOME:

http://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/index.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 2
    Thanks Juned it worked , but why jdk not jre...and why ant was working and maven wasn't. – paul Jun 26 '13 at 09:45
  • thanks, it's worked! For OSX path we can read here: http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/ – Francesco Oct 07 '15 at 14:04
  • This worked for me https://www.mkyong.com/java/how-to-set-java_home-on-windows-10/ – Jeppen Nov 20 '19 at 18:29
16

Do not include bin in your JAVA_HOME env variable

sadhu
  • 1,429
  • 8
  • 14
8

Follow the instruction in here.

JAVA_HOMEshould be like this

JAVA_HOME=C:\Program Files\Java\jdk1.7.0_07
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
5

JAVA_HOME = C:\Program Files\Java\jdk(JDK version number)

Example: C:\Program Files\Java\jdk-10

And then restart you command prompt it works.

karthik padav
  • 248
  • 4
  • 2
  • 1
    thank you for this. been struggling for hours with this until i read the restart part. don't forget to restart, cuz even if you change it so many times it won't work until you do. – gdubs Aug 07 '18 at 02:19
  • Thank you. After setting was struggling for minutes. Restart is required. – aatif Feb 02 '21 at 19:14
4

JAVA_HOME should point to jdk directory like in the image with new variable, like below

JAVA_HOME

PATH should point to jdk bin like below

PATH

Loki
  • 1,180
  • 9
  • 13
2

Just remember to add quotes into the path if you have a space in your path to java home. C:\Program Files\java\javaxxx\ doesn't work but "C:\Program Files\java\javaxxx\" does.

jonorri
  • 21
  • 3
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Avi Jun 04 '14 at 22:51
  • Ahhhh I see. Just wanted to leave this here since I ended up experiencing the problem with the answer above from Ruchira Gayan. "JAVA_HOME should be like this JAVA_HOME=C:\Program Files\Java\jdk1.7.0_07" And I had to wrap the path with quotes. But comment noted. Will come back when I have any questions. – jonorri Jun 09 '14 at 15:01
2

Run the below command in your terminal and restart it.

> set JAVA_HOME="C:\Program Files\Java\jdk-xx.xx"

xx.xx is the java version

codeX
  • 4,842
  • 2
  • 31
  • 36
1

The JAVA_HOME should point to the JDK home rather than the JRE home if you are going to be compiling stuff, likewise - I would try and install the JDK in a directory that doesn't include a space. Even if this is not your problem now, it can cause problems in the future!

nwaltham
  • 2,067
  • 1
  • 22
  • 40
1

You are pointing your JAVA_HOME to the JRE which is the Java Runtime Environment. The runtime environment doesn't have a java compiler in its bin folder. You should download the JDK which is the Java Development Kit. Once you've installed that, you can see in your bin folder that there's a file called javac.exe. That's your compiler.

JREN
  • 3,572
  • 3
  • 27
  • 45
0

JAVA_HOME should point to the home jdk directory and not to jdk/bin directory.

Make sure you have JDK installed in your computer and then you need to set the JAVA_HOME like this:

JAVA_HOME="C:\Program Files\Java\jdk" + "Your current installed version of jdk in your system"
0

After installing java , you have to make two changes in environment variables .

  1. JAVA_HOME : which should point to jdk installation directory (not including bin) .
  2. Path : Which will put the bin on the path .

if the error is which respect to JAVA_HOME , try below :

  1. Ensure you have mentioned the directory path correctly .

  2. JAVA_HOME value ending with semicolon could be the ERROR sometimes.If you are not able to remove semicolon from environment, try using cmd as below

    set JAVA_HOME = [path of you jdk installation directory ]

toyota Supra
  • 3,181
  • 4
  • 15
  • 19