Maven doesn’t detect JAVA_HOME correctly on Ubuntu
I was playing around with some environmental variables in Ubuntu 18.04.5, trying to make them not reset every time I re-open terminal. However, now Maven is reacting to every command with this 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
I’m pretty sure that my $JAVA_HOME points to the right directory, because command echo $JAVA_HOME returns:
/usr/lib/jvm/java-11-openjdk-amd64
openjdk 11.0.8 2020-07-14 OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1) OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)
export JAVA14="/usr/lib/jvm/jdk-14" export JAVA11="/usr/lib/jvm/java-11-openjdk-amd64" export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Also, after some editing I’ve returned everything as it was before and command sudo update-alternatives —config java return the following:
Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64 1 manual mode 2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
So the right path is selected, and also I have not changed any other update-alternatives configurations. What can I do to make Maven see $JAVA_HOME again? EDIT: I’ve fixed this issue by directly editing the /usr/share/maven/bin/mvn script and hard-coding path to java executable into JAVACMD variable. However, I’m still interested in normal solution to this problem.
JAVA_HOME should point to a JDK not a JRE
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
«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.
26 Answers 26
Control Panel -> System and Security -> System -> Advanced system settings -> Advanced -> Environment Variables -> New System Variable
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.
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.
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.
Awesome! to unset, I did: unset JAVA_HOME . Then vi ~/.bash_profile and add export JAVA_HOME=»/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home» Only change the actual path in your MAC
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.
This was my solution. I had a previous entry for JAVA_HOME in User variables and removing it solved the issue.
echo %JAVA_HOME% set set JAVA_HOME=C:\Program Files\Java\jdk1.8.0 echo %JAVA_HOME%
I added JAVA_HOME path in user variable and omit the «/bin». I tried every method given here but only this worked for me.
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. 😀
My JAVA_HOME was set correctly but I solved this issue by running Command Prompt as Administrator
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-
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
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
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 .
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
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.
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
Make sure that you do NOT have a JRE path, if you have delete it.
- Add JAVA_HOME in the System variable. Variable value: C:\Program Files\Java\jdk-10.0.2 (location of JDK without bin)
- Add M2 in the System variable. Variable value: C:\dev\maven\apache-maven-3.5.4\bin (location of maven with bin)
- Add M2_HOME in the System variable. Variable value: C:\dev\maven\apache-maven-3.5.4 (location of maven without bin)
- 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.
Please note that since Maven 3.5.0 the usage of M2_HOME has been removed and is not supported any more (maven.apache.org/docs/3.5.0/release-notes.html)
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.
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
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%
you should only add this path to Manage Jenkins -> Global Tool Configuration -> JDK
And then use same in your jenkins jobs accordingly
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
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
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
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
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.
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"
Why does maven ignores the JDK i installed?
My JAVA_HOME points at the bin folder of this JDK. My PATH variable also has the same bin folder in it. My question is what am I doing wrong? Shouldnt java -version say Development kit instead of Runtime enviroment?
JAVA_PATH is not a thing. You need to define JAVA_HOME to point to the root folder of your Java installation e.g. /home/user/java/1.8.0_212-1-ojdkbuild
You can also add JAVA_HOME\bin (or JAVA_HOME/bin) to your path. That makes the java executable available everywhere and that’s how java -version can work
Did you set it OS-wide? It could be mvn doesn’t see the environment variable. BTW what OS are you on?
4 Answers 4
I don’t think we are going to work out what the problem by asking you questions. Instead I suggest you do the following:
- At the command prompt, set the environment variable %MAVEN_BATCH_ECHO% to the value on . Then at the same command prompt, run mvn . This is supposed to echo all commands that the mvn.cmd batch file runs.
- Compare the output with the source of the batch file . as installed on your system. In particular, see what gets echo’d for these lines:
@REM ==== START VALIDATION ==== if not "%JAVA_HOME%"=="" goto OkJHome for %%i in (java.exe) do set "JAVACMD=%%~$PATH:i" goto checkJCmd :OkJHome set "JAVACMD=%JAVA_HOME%\bin\java.exe" :checkJCmd if exist "%JAVACMD%" goto chkMHome echo The JAVA_HOME environment variable is not defined correctly >&2 echo This environment variable is needed to run this program >&2 echo NB: JAVA_HOME should point to a JDK not a JRE >&2 goto error
Note that the validation is actually checking that %JAVA_CMD% exists as a file.
I see you have actually solved the problem by reinstalling. Unfortunately, that doesn’t tell us what the problem really was.