109

I am trying to set up maven for my project and I am getting this error

"JAVA_HOME should point to a JDK not a JRE"

I know there are already similar question but it did not work. How can I point JAVA_HOME to JDK in windows. I am using IntelliJ IDEA

hennr
  • 2,632
  • 2
  • 23
  • 26
Abdurakhmon
  • 2,813
  • 5
  • 20
  • 41
  • 2
    First you need to install a JDK. Have you done that? – Stephen C Apr 19 '17 at 12:50
  • https://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/, see also http://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7 –  Apr 19 '17 at 12:50
  • You rarely have to define JAVA_HOME at all. – Klitos Kyriacou Apr 19 '17 at 12:51
  • 5
    *"I know there are already similar question but it did not work."* == "Please downvote me". (But don't bother shooting the messenger; I'm not voting on this question.) If you tried something and it didn't work, you need to specify exactly what you tried, exactly what behavior you expected, and exactly what behavior you got instead. – Mark Adelsberger Apr 19 '17 at 12:54
  • 2
    @KlitosKyriacou - Maven requires the JAVA_HOME environment variable to be set – Steve C Apr 19 '17 at 13:05
  • I had similar issues on container and the solution was to find all the locations of java and try other locations for Java_HOME – SGuru Feb 13 '22 at 23:29

26 Answers26

93

Control Panel -> System and Security -> System -> Advanced system settings -> Advanced -> Environment Variables -> New System Variable

enter image description here

Shreyas
  • 999
  • 6
  • 19
sovas
  • 1,508
  • 11
  • 23
  • 64
    For me it was important to note that the path should not contain the *bin* – wake-0 Nov 07 '17 at 13:04
  • 3
    Thanks a lot i was making the same mistake of including bin directory, and was getting maven error when trying to maven clean install my project. – Rajan Chauhan Dec 20 '17 at 18:06
  • Not in the original question but in Ubuntu 18.04 you can do something similar by setting the environment variable via: `export JAVA_HOME='/usr/lib/jvm/java-11-openjdk-amd64'` as an example of the jdk I have installed on my machine. Replace with yours. – Pablo Adames Aug 02 '22 at 01:01
46

I am going through the same process on Mac OSX. I installed the latest JDK, then installed Maven. Someone suggested I set the JAVA_HOME variable so I pointed it to the JDK installation folder. When running Maven mvn compile exec:java I received the same error NB: JAVA_HOME should point to a JDK not a JRE.

All I did was unset the JAVA_HOME variable and it worked.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dan Mergens
  • 560
  • 1
  • 4
  • 7
11

I met the same problem. (Window 10 environment) I solved it by deleting the JAVA_HOME="C:\Program Files\Java\jdk1.8.0_161\bin" in the User Variables instead of adding to the System Variables directly.

Then I test that editing JAVA_HOME="C:\Program Files\Java\jdk1.8.0_161\" worked too. When I run "mvn -version" in command prompt window, it shows "Java home: C:\Program Files\Java\jdk1.8.0_161\jre".

In conclusion, I guess the JAVA_HOME shouldn't include bin directory.

Rui Sun
  • 111
  • 1
  • 2
  • 2
    This was my solution. I had a previous entry for JAVA_HOME in User variables and removing it solved the issue. – Joe Jordan Apr 11 '19 at 17:07
10

do it thru cmd -

echo %JAVA_HOME% set set JAVA_HOME=C:\Program Files\Java\jdk1.8.0 echo %JAVA_HOME%

Begineer
  • 191
  • 3
  • 3
7

I added JAVA_HOME path in user variable and omit the "/bin". I tried every method given here but only this worked for me.

Zoe Zoe
  • 71
  • 1
  • 1
5

I have spent 3 hours for solving the error The JAVA_HOME environment variable is not defined correctly. This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE

Finally I got the solution. Please set the JAVA_HOME value by Browse Directory button/option. Try to find the jdk path. Ex: C:\Program Files\Java\jdk1.8.0_181

It will remove the semicolon issue. :D Browse Directory option

King Midas
  • 1,442
  • 4
  • 29
  • 50
Tejashree
  • 750
  • 12
  • 14
5

My JAVA_HOME was set correctly but I solved this issue by running Command Prompt as Administrator

Archmede
  • 1,592
  • 2
  • 20
  • 37
4

In Mac OS the hierarchy library > java > JavaVirtualMachines - (inside this folder there are different versions of jdk) select your desired version and inside jdk-version folder there is a contents folder inside contents you'll find "Home" folder while declaring $JAVA_HOME you haVE TO GIVE THAT HOME PATH for example-

(normal shell commands) open terminal type vi .bash_profile (to open file name bash_profile) press "i" to enable insert mode give java home path as-

export JAVA_HOME=/Library/java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

After editing press esc to exit editing mode then :wq to quit and save

This will remove JAVA_HOME should point to a JDK not a JRE error and also saves you from future errors

clubby789
  • 2,543
  • 4
  • 16
  • 32
4

For mac OS, this worked for me... none of the above solutions

$ vim .bash_profile

export JAVA_HOME=$(/usr/libexec/java_home)

$ source .bash_profile
niti
  • 129
  • 1
  • 6
2

Be sure to use the correct path!

I mistakenly had written C:\Program Files\Java\. Changing it to C:\Program Files\Java\jdk\11.0.6\ fixed the issue.

In cmd I then checked for the version of maven with mvn -version.

Joe McMahon
  • 3,266
  • 21
  • 33
Melika
  • 21
  • 3
  • The path was my issue as well. I had `JAVA_HOME` set to `C:\Program Files\Java\jdk-11.0.7\bin` rather than `C:\Program Files\Java\jdk-11.0.7` – AaronK Oct 26 '21 at 13:31
2

Just as an addition to other answers

For macOS users, you may have a ~/.mavenrc file, and that is where mvn command looks for definition of JAVA_HOME first. So check there first and make sure the directory JAVA_HOME points to is correct in that file.

黄锐铭
  • 311
  • 4
  • 5
1

if You have The JAVA_HOME environment variable is not defined correctly This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE Error so do one thing ...type C:>dir/x and you will see the PROGRA~1 or May ~2 and After int Environment Variable Chang The JAVA_HOME Dir Like This JAVA_HOME:- C:\PROGRA~1\Java\jdk1.8.0_144\ also Set In Path :-%JAVA_HOME%\bin; And it Works

1

Make sure that you do NOT have a JRE path, if you have delete it.

  1. Add JAVA_HOME in the System variable. Variable value: C:\Program Files\Java\jdk-10.0.2 (location of JDK without bin)
  2. Add M2 in the System variable. Variable value: C:\dev\maven\apache-maven-3.5.4\bin (location of maven with bin)
  3. Add M2_HOME in the System variable. Variable value: C:\dev\maven\apache-maven-3.5.4 (location of maven without bin)
  4. Add %JAVA_HOME% and %M2% in Path System Variable or C:\Program Files\Java\jdk-10.0.2 and C:\dev\maven\apache-maven-3.5.4\bin --> For windows 10, just add the location. For other version, at the end of the Variable Value field add semicolon then the location Ex: ;%JAVA_HOME%;%M2%

I did not check if the addition or removal of bin changes the result but nonetheless this works for me.

wibeasley
  • 5,000
  • 3
  • 34
  • 62
AlTur
  • 11
  • 1
  • 1
    Please note that since Maven 3.5.0 the usage of `M2_HOME` has been removed and is not supported any more (https://maven.apache.org/docs/3.5.0/release-notes.html) – Robert Hume Dec 10 '18 at 15:51
1

In addition to sovas' response on how to add the JAVA_HOME variable, if it was working before and stopped working, ensure that the path still exists. I updated Java recently which deleted the old version, invalidating my JAVA_HOME environment variable.

Matthew Woo
  • 1,288
  • 15
  • 28
1

Under System Variables add below

JAVA_HOME = C:\Program Files\Java\jdk1.8.0_201

JDK_HOME = %JAVA_HOME%\bin

M2_HOME = C:\apache-maven-3.6.0

MAVEN_BIN = %M2_HOME%\bin

MAVEN_HOME = %M2_HOME%

Under path Add these

%M2_HOME%

%JDK_HOME%
Archmede
  • 1,592
  • 2
  • 20
  • 37
1

This worked for me for Windows 10, Java 8_144.

If the path contains spaces, use the shortened path name. For example, C:\Progra~1\Java\jdk1.8.0_65

RahulDeep Attri
  • 398
  • 3
  • 13
1

you should only add this path to Manage Jenkins -> Global Tool Configuration -> JDK

for java 11

/usr/lib/jvm/java-11-openjdk-amd64

for java 8

/usr/lib/jvm/java-8-openjdk-amd64

And then use same in your jenkins jobs accordingly

Kumar Pankaj Dubey
  • 1,541
  • 3
  • 17
  • 17
0

In IntelliJ IDEA go to File>Project Structure>SDK>JDK home path. Copy it and then go to My Computer>Advanced Settings>Environment Variables Change the JAVA_HOME path to what you have copied. Then open new cmd, and try mvn -v

It worked for me !!!

Salman S
  • 47
  • 1
  • 15
0

Add JAVA_HOME = C:\Program Files\Java\jdk(version) in User variable, it works for me. For me, it doesn't work with bin and even if I create JAVA_HOME in system variable

Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
Gautam Shahi
  • 455
  • 4
  • 14
0

just remove the semicolon at the end of JAVA_HOME variable's value.

set JAVA_HOME as C:\Program Files\Java\jdk1.8.0_171

It worked for me.

anothernode
  • 5,100
  • 13
  • 43
  • 62
0

I had this issue but for Mac Os, I set the JAVA_HOME variable in the .bash_profile to be export JAVA_HOME=$(/usr/libexec/java_home) then save. After that ran source ~/.bash_profile finally mvn -version and it fixed the issue. Hope that helps

Salma Elshahawy
  • 1,112
  • 2
  • 11
  • 21
0

Windows 10 Home for me:

I'm studying maven through a udemy course. First time environment variables were ok. I had on JAVA_HOME on SYSTEM VARIABLE like this:

D:\Install\Java\jdk-12.0.1;D:\Install\apache-maven-3.5.4-bin\apache-maven-3.5.4

After some days, don't know what's happened, I began to receive:

C:\Users\Franco>mvn -version

The JAVA_HOME environment variable is not defined correctly

This environment variable is needed to run this program

NB: JAVA_HOME should point to a JDK not a JRE

After trying all above, I tried to delete jdk the entry on SYSTEM VARIABLES, and putting it on USER VARIABLES, so now I have:

JAVA_HOME on USER VARIABLES: D:\Install\Java\jdk-12.0.1

JAVA_HOME on SYSTEM VARIABLES: D:\Install\apache-maven-3.5.4-bin\apache-maven-3.5.4

now restarting CMD I have:

C:\Users\Franco>mvn -version

Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00)

Maven home: D:\Install\apache-maven-3.5.4-bin\apache-maven-3.5.4\bin\..

Java version: 12.0.1, vendor: Oracle Corporation, runtime: D:\Install\Java\jdk-12.0.1

Default locale: en_US, platform encoding: Cp1252

OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
Zoe
  • 27,060
  • 21
  • 118
  • 148
Falco
  • 1,458
  • 3
  • 19
  • 47
0

First, ensure that the Maven bin is in your Environmental Variable PATH entry.

If it is, make sure your entries aren't somehow out of order, and that JAVA_HOME is before Path in the list, or any entry that references %JAVA_HOME%. I was getting the same error when I was trying to check my maven version.

I have a few extra path variables that reference %JAVA_HOME%, or a different version of a JDK and Maven was mixed in between. I moved my Maven path entry below my %JAVA_HOME% one and now everything is working when I use Maven from cmd.

But it is Windows, so perhaps my just opening and closing the Environment Variables setting somehow made everything better.

jon.bray.eth
  • 527
  • 6
  • 13
0

In my case the error started showing up as Java version got updated. So JAVA_HOME path became invalid.

Please check

  1. Location referred to in JAVA_HOME exists.
  2. If it's like my case, update the value from the old path to the new path.

(A better fix would be to set java update such that it auto upgrades the JAVA_HOME reference.)

Randall
  • 725
  • 9
  • 27
0

Even after trying this solution from sovas which is accepted if it does not work

RESTART intellij / CMD prompt instead of trying on the existing opened

i was trying the command from terminal under intellij but still getting same problem.

Do reopen cmd / INTELLIJ and the variable will get reloaded. It fixed the problem for me (ofcourse after correcting the Path to JDK not jre and removing the bin

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
0

In case you have the same error on Win10 and OpenJDK. The cause of my issue is that the OpenJDK installation set the JAVA_HOME variable for me, but set it pointing to a wrong path:

cause during the openjdk installation

When I manually edited this environment variable, the path was pointing to: • C:\Program Files\Eclipse Foundation\jdk-16.0.2.7-hotspot (Wrong) Wrong path

Instead I changed it to: C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot (Correct) Correct path

technik
  • 1,009
  • 11
  • 17