113

Issue:

I am getting the Maven error "The JAVA_HOME environment variable is not defined correctly" when I run through Inno setup batch execution. However, I am able to run successfully outside Inno Setup.(e.g Command line, Batch file, Vbs). I am clueless to identify the issue.

Inno Setup Invoked Prompt:

C:\>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

Regular Command Prompt:

C:\>mvn -version
C:\
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T01:09:06+05:30)
Maven home: C:\Program Files\apache-maven-3.5.0\bin\..
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_131\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

Maven Command from InnoSetup:

[Files]
Source: "C:\@Setup\MavenInstaller.bat"; DestDir: "{tmp}"; Flags: ignoreversion
[Run]
Filename: "{cmd}"; Parameters: "/C ""{tmp}\MavenInstaller.bat"""

Maven Command from Batch File:

mvn archetype:generate -DgroupId=com.mycompany.mycomponent-DartifactId=%APPLICATION_NAME% -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Setting JAVA_HOME

Check the If Java 1.8 is installed or not. If not installed, Install the same and set the JAVA HOME as follows.

SETX JAVA_HOME "C:\Program Files\Java\jdk1.8.0_131"
SETX -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0_131"
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%path%;C:\Program Files\Java\jdk1.8.0_131\bin;" /f

Many Thanks.

ramkumar-yoganathan
  • 1,918
  • 3
  • 13
  • 29

33 Answers33

91

Following is the best way to get of the issue , check following on classpath:

  1. Make sure JAVA_HOME system variable must have till jdk e.g C:\Program Files\Java\jdk1.7.0_80 , don't append bin here.

  2. Because MAVEN will look for jre which is under C:\Program Files\Java\jdk1.7.0_80

  3. Set %JAVA_HOME%\bin in classpath .

Then try Maven version .

Hope it will help .

xalien
  • 153
  • 1
  • 7
mukesh sharma
  • 911
  • 6
  • 2
63

My JDK is installed at C:\Program Files\Java\jdk1.8.0_144\.
I had set JAVA_HOME= C:\Program Files\Java\jdk1.8.0_144\, and I was getting 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

When I changed the JAVA_HOME to C:\Program Files\Java\jdk1.8.0_144\jre, the issue got fixed.
I am not sure how.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Koushik
  • 639
  • 4
  • 2
  • 2
    Hi there, welcome to Stack Overflow! I edited your answer to make the code parts more evident, and easier for Stack Overflow users to read - please see the editing help for more information on formatting. Thanks for sharing! – SherylHohman Aug 19 '17 at 05:45
  • 1
    This is work, thank you. But how can this work using directory `jre` instead `bin`? – NM Naufaldo Jun 06 '20 at 09:17
  • You might need to restart the bash/cmd for it to reflect changes. – Vibhor Dube Aug 09 '20 at 05:59
  • This has worked for me as well. Since I installed the JDK via IntelliJ IDEA it saved it on `%HOMEPATH%\.jdks` so my path that I had to add to the environment variables ended up being `%HOMEPATH%\.jdks\corretto-1.8.0_322\jre`. Thanks : ) – badjuice Mar 29 '22 at 13:13
54

This is how I fixed this issue on Windows 10:

My JDK is located in C:\Program Files\Java\jdk-11.0.2 and the problem I had was the space in Program Files. If I set JAVA_HOME using set JAVA_HOME="C:\Program Files\Java\jdk-11.0.2" then Maven had an issue with the double quotes:

C:\Users>set JAVA_HOME="C:\Program Files\Java\jdk-11.0.2"

C:\Users>echo %JAVA_HOME%
"C:\Program Files\Java\jdk-11.0.2"

C:\Users>mvn -version
Files\Java\jdk-11.0.2""=="" was unexpected at this time.

Referring to Program Files as PROGRA~1 didn't help either. The solution is using the PROGRAMFILES variable inside of JAVA_HOME:

C:\Users>echo %PROGRAMFILES%
C:\Program Files

C:\Program Files>set JAVA_HOME=%PROGRAMFILES%\Java\jdk-11.0.2

C:\Program Files>echo %JAVA_HOME%
C:\Program Files\Java\jdk-11.0.2

C:\Program Files>mvn -version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T17:06:16+02:00)
Maven home: C:\apache-maven-3.6.2\bin\..
Java version: 11.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-11.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
nocdib
  • 1,286
  • 10
  • 7
16

A very common mistake people make is that, when they set JAVA_HOME or JRE_HOME, they set the value to C:\Program Files\Java\jdk1.8.0_221\bin or similar.

Please note JAVA_HOME and JRE_HOME value should not contain \bin

theduck
  • 2,589
  • 13
  • 17
  • 23
Sachin Singh
  • 383
  • 3
  • 10
9

The SETX command does not modify the current environment.

If you run the following batch file:

setx AAA aaa
echo AAA=%AAA%

It will print

AAA=

So your batch file is wrong. You have to use set:

set AAA=aaa

See What is the difference between SETX and SET in environment variables in Windows.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
7

removing JAVA_HOME and JAVA_JRE from environment variable is resolved the issue.

jaibalaji
  • 3,159
  • 2
  • 15
  • 28
6

I was facing the same issue while using mvn clean package command in Windows OS

C:\eclipse_workspace\my-sparkapp>mvn clean package
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 resolved this issue by deleting JAVA_HOME environment variables from User Variables / System Variables then restart the laptop, then set JAVA_HOME environment variable again.

Hope it will help you.

Prashant Sahoo
  • 979
  • 16
  • 19
  • In my laptop at work it was enough to just changing the JAVA_HOME and closing and re-opening the console. But in my laptop at home I had not restart the laptop after changing the JAVA_HOME. – Juan José Melero Gómez Nov 24 '18 at 13:37
6

Setting JAVA_HOME directory from command line worked for me!

First:

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

Or :

export JAVA_HOME="C:\Program Files\Java\jdk1.8.0"

Then try:

mvn -version

to make sure you do not get the same error. :)

dicle
  • 1,122
  • 1
  • 12
  • 40
5

I have removed JAVA_HOME variable and kept only path and classpath variables by pointing them to jdk and jre respectively. It worked for me.

Shivakumar
  • 51
  • 1
  • 1
5

In your environment variable path, add this

%JAVA_HOME%\bin

and also add your path of Maven folder like this

C:\Program Files (x86)\apache-maven-3.8.4\bin

Then create user variable named

JAVA_HOME 

and set as path

C:\Program Files\Java\jdk-17\ 

(wihout bin!)

Last step: important Restart IntelliJ or another program you are using.

Matteo Toma
  • 562
  • 5
  • 12
4

I was having this same issue while my JAVA_HOME system variable was pointing to C:\Program Files\Java\jdk1.8.0_171\bin and my PATH entry consisted of just %JAVA_HOME%.

I changed my JAVA_HOME variable to exclude the bin folder (C:\Program Files\Java\jdk1.8.0_171), and added the bin folder to the system PATH variable: %JAVA_HOME%\bin,

slashNburn
  • 464
  • 4
  • 17
4

I had the same problem on Windows 7.

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

The solution turned out to be very simple - right click on command prompt shortcut and choose "Run as Administrator". After that, the problem disappeared)

Yurii Koval
  • 299
  • 1
  • 7
3

I struggled with the same issue, and the following worked for me.

Step 1: Check your JAVA_HOME setting. It may look something like:

JAVA_HOME="/usr/libexec/java_home"

Step 2: Update JAVA_HOME like so:

$ vim .bash_profile 

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

$ source .bash_profile

Step 3: In a new shell, check that the Maven command is now working properly:

$ mvn -version

If this fixed the problem, you should get back a response like:

Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)
Maven home: /usr/share/maven
Java version: 1.7.0_05, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.2", arch: "x86_64", family: "mac"
d_dawg
  • 31
  • 1
3
SET JAVA_HOME=C:\Program Files\Java\jdk1.8.0

worked fine for me.

Note - Don't put double quotes over the path as mentioned above. Otherwise when you run

mvn -version

it will give following error

Files\java\jdk1.8.0_201\jre""=="" was unexpected at this time.

Maximouse
  • 4,170
  • 1
  • 14
  • 28
surajmall
  • 178
  • 1
  • 7
2

when you setup the java home variable try to target path till JDK instead of java. setup path like: C:\Program Files\Java\jdk1.8.0_231

if you make path like C:\Program Files\Java it will run java but it will not run maven.

Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
abhirup
  • 21
  • 1
2

Removing JAVA_HOME from environment variables solved the issue for me.

pasindupa
  • 689
  • 9
  • 10
1

Following steps solved the issue for me..

  • Copied the zip file into the Program Files folder and extracted to "apache-maven-3.6.3-bin".

  • Then copied the path, C:\Program Files\apache-maven-3.6.3-bin\apache-maven-3.6.3

  • Then created the new MAVEN_HOME variable within environmental variables with the above path.

Also added,

C:\Program Files\apache-maven-3.6.3-bin\apache-maven-3.6.3\bin

address to the "PATH" variable

menaka_
  • 1,092
  • 1
  • 13
  • 23
Sanjeewa
  • 13
  • 4
1
  1. create folder Maven inside this folder extract download file

  2. this file should C:\Program Files\YourFolderName must in C:\Program Files drive

  3. goto This PC -> right click -> properties -> advanced system -> environment variable

  4. user variable ----> new & ** note create two variable ** if not may be give error i) variable name = MAVEN variable value = C:\Program Files\MAVEN

    ii) variable name = MAVEN_HOME variable value = C:\Program Files\MAVEN\apache-maven-3.6.3\apache-maven-3.6.3

  5. system variable path ---> Edit---> new----give path of this folder i) C:\Program Files\MAVEN
    ii) C:\Program Files\MAVEN\apache-maven-3.6.3\bin

Hurrraaaaayyyyy

1

I have run the below command as Administrator to solve the issue:

SETX JAVA_HOME "C:\Program Files\Java\jdk-15.0.2"
SETX -m JAVA_HOME "C:\Program Files\Java\jdk-15.0.2"
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%path%;C:\Program Files\Java\jdk-15.0.2\bin;" /f
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

Easy Win10 solution After JDK installation, download maven and paste to C:/, then copy its path and paste to "environment variables". Go to "windows key" on keyboard an type "environment variables". Click button "environment variables" on right-bottom corner.

From "system variables" choose JAVA_HOME and click "edit" Probably you have path to Java JRE instead of JDK. Check where you install JDK (in my case C:\Program Files\Java\jdk-17 ) and paste that path.

REMEMBER!! JDK path should not be path to bin folder because you will get double bin like C:\Program Files\Java\jdk-17\bin\bin\java because Maven check (see else condidion) :

if [ -z "$JAVA_HOME" ] ; then
    JAVACMD=`which java`
else
    JAVACMD="$JAVA_HOME/bin/java"
fi
 
if [ ! -x "$JAVACMD" ] ; then
    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
    exit 1
fi
anyapps
  • 91
  • 2
  • 6
1

if you are using mac...

  1. install JDK

  2. config JAVA_HOME by vim ~/.bash_profile like this, change the jdk file path to your own

    JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME PATH CLASSPATH

  3. source ~/.bash_profile and then check by java -version and mvn install

  4. if it does not work, restart your computer and check again.

i solved this problem by restarting my computer...

0

In case of windows if there is any space in path to jdk like ("C:\Program Files\jdk") then it doesn't work, but if we keep jdk in a location which doesn't have space then it works fine like ("C:\jdk")

0

It seems that Maven doesn't like the JAVA_HOME variable to have more than one value. In my case, the error was due to the presence of the additional path C:\Program Files\Java\jax-rs (the whole path was C:\Program Files\Java\jdk1.8.0_20;C:\Program Files\Java\jax-rs).

So I deleted the JAVA_HOME variable and re-created it again with the single value C:\Program Files\Java\jdk1.8.0_20.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
0

I was able to solve this problem with these steps:

  1. Uninstall JDK java
  2. Reinstall java, download JDK installer
  3. Add/Update the JAVA_HOME variable to JDK install folder
Yousra ADDALI
  • 332
  • 1
  • 3
  • 14
0

Follow these steps to fix it:

My jdk location: C:\Program Files\Java\jdk1.8.0_60\bin

C:\Users>echo %PROGRAMFILES% C:\Program Files

C:\Program Files>set JAVA_HOME=%PROGRAMFILES%\Java\jdk1.8.0_60

C:\Program Files>echo %JAVA_HOME% C:\Program Files\Java\jdk1.8.0_60

C:\Program Files>mvn -version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: E:\java\apache-maven-3.6.3\bin.. Java version: 1.8.0_60, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_60\jre Default locale: en_IN, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

loopswager
  • 41
  • 3
0

You need to have the full path to the JDK folder. IF you use links like ~ in your path replace it with /home/<your_user>/, or it will give this error.

Luis Miguel Serrano
  • 5,029
  • 2
  • 41
  • 41
0

Do not add \bin when adding path

do

C:\Program Files\Java\jdk-15.0.2

do not..

C:\Program Files\Java\jdk-15.0.2\bin

Rahat Shah
  • 119
  • 1
  • 4
0

Your JAVA_HOME needs to be set where the jdk is present C:\Program Files\Java\jdk-11.0.13 In the path you could use %JAVA_HOME%/bin

PS : Also check if you are pointing to the right version of jdk in your environment variables. In my case I had jdk-11.0.13 but had outdated path in the env variable which was still pointing to C:\Program Files\Java\jdk1.8.0_301.

Bhoomi
  • 17
  • 4
0

You can set JAVA HOME by following commands in CMD

setx JAVA_HOME "C:\Program Files\Java\jdk-11.0.17"
setx PATH "%PATH%;%JAVA_HOME%\bin";

Make sure to close all CMD instances and then open them again to check.

Code Spy
  • 9,626
  • 4
  • 66
  • 46
0

also, sometimes it might cause some problems if the java_home variable is not included in the user variables for administrators' section. So also add them there if you are logged into the administrators account.

as you know, java home var should not have the bin part while the path should point to the bin.

Wanjalize
  • 21
  • 2
0

Linux Users

For Linux users, export the path to the base of the installation directory of your JDK.

For example

export PATH=/data/gadgets/opt/java/openjdk/current/bin/:$PATH

Please note that this is not the typical location on most Linux systems. However, you can find the location by searching for javac:

find / -type f -name 'javac' 2>/dev/null

which will print a list of filenames matching javac. There will likely be only one, but there can be more than one if you have multiple JDKs installed.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
-1

add following on env path:

  1. Make sure JAVA_HOME system variable e.g C:\Program Files\Java\jdk , don't append bin here.

  2. Restart your computer

MonirRouissi
  • 549
  • 8
  • 7
-5

Also Please make sure your java, mvn and all installations are in C Directory

Prashant
  • 17
  • 1
  • 6