JAVA_HOME should point to a JDK not a JRE - java

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

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

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.

do it thru cmd -
echo %JAVA_HOME%
set set JAVA_HOME=C:\Program Files\Java\jdk1.8.0
echo %JAVA_HOME%

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.

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. :D

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-
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

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.

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.

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%
Under path Add these
%M2_HOME%
%JDK_HOME%

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

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 !!!

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
It worked for me.

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.

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"

In my case the error started showing up as Java version got updated. So JAVA_HOME path became invalid.
Please check
Location referred to in JAVA_HOME exists.
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.)

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

Related

Why does maven ignores the JDK i installed?

When I do anything with Maven (using mvn command) I get the following message:
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
java -version
returns the following:
openjdk version "1.8.0_212-1-ojdkbuild"
OpenJDK Runtime Environment (build 1.8.0_212-1-ojdkbuild-b04)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)
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?
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 above excerpt is taken from the most recent version of the Maven batch file does. You may have an older version of Maven that does something different. Check it before jumping to conclusions.)
If that doesn't work, modify the batch file and add a line to echo what %JAVA_CMD% is set to. Then run the modified batch file.
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.
JAVA_HOME should refer to folder where bin folder is located, but I think you have installed inly java, without jdk, run javac -version you will see if compiler is available.
Probably you should install package openjdk-8-jdk, but you have only openjdk-8-jre. Thats ubuntu package names. Name can be different depending on your OS.
It seems you have installed JDK under C:\Program Files\... and therefore you need to put the absolute path in both, JAVA_HOME and PATH environment variables i.e. do not set %JAVA_HOME%\bin in PATH; rather, set C:\Program Files\...\bin in PATH environment variable.
Also, make sure the path set in JAVA_HOME is one level above the path set in PATH variable.
Last but not the least, make sure you move the path of this JDK above all values under PATH which will make sure that the path of another JDK does not take precedance over the JDK which you want to work with.
After uninstalling everything and clearing directories and computer restart. I reinstalled everything. In System variables PATH is set to java path + /bin and on JAVA_HOME its just the path to the folder.
MAven is operational again

Error Launching IntelliJ--does not point to a valid JVM

I have declared JAVA_HOME in environment variables and I have the following value in PATH: C:\Java\jre1.8.0_45\bin;%JAVA_HOME%;
But still while launching IntelliJ I am getting below error.
The values from command prompt:
PS: This is a different problem. But the one I am facing does not have any such issue.
I have JRE in my path variable and JDK in JAVA_HOME.(attached the screenshots)
Your %JAVA_HOME% variable should point to the directory of Java installation. Here's what you can do to fix it.
Set %JAVA_HOME% to C:\Java\jre1.8.0_45\
Add %JAVA_HOME%\bin to your %PATH% variable
you can also add JDK to your system variables and point it to the JRE folder (do not point it to BIN folder)

Trying to run Maven but JAVA_HOME not found [duplicate]

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.
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
Do not include bin in your JAVA_HOME env variable
Follow the instruction in here.
JAVA_HOMEshould be like this
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_07
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.
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.
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!
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.
JAVA_HOME should point to jdk directory like in the image with new variable, like below
PATH should point to jdk bin like below
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

Ionic failed to build for Android, JAVA_HOME invalid, failed to run java -version

I already tried to set my JAVA_HOME to C:\Program Files\Java\jdk1.8.0_73
and the path is C:\Program Files\Java\jdk1.8.0_73\bin.
The goal of what you are asking to do is to install java and let some other program find java by setting two Windows variables JAVA_HOME and path to point to the java directory and the java executable/program file directory respectively. I assume you are using windows because of some of the clues you gave. You first need to download java, get the JDK and not the JRE version. Download from herejava then set the two variables, find mycomputer icon or maybe its called this pc and right click find properties click advanced system settings, environment variables there you go, put in the variable and value for both JAVA_HOME and path
I had the same problem and had solved it.
The cause of problem changing the path when installing jdk. Changed jre path to
../jdk1.8.0/jre
the right is for example:
../java/jdk1.8.0
../java/jre8
or change nothing.
So, if you are sure that JAVA_HOME path is right, try to reinstall jdk and change nothing before complete.

Setting up dev env for Cordova on Windows: ant does not recognize JAVA_HOME

I have been trying to set up Cordova on my Windows 7 machine. After hours of troubleshooting I believe I have narrowed the problem down to the configuration of ant, but I'm at a loss as to how to fix it.
When executing cordova build from the prompt, under -compile: I get:
BUILD FAILED
C:\path\to\ant\build.xml:601: The following error occurred while executing this line:
C:\path\to\ant\build.xml:720: The following error occurred while executing this line:
C:\path\to\ant\build.xml:734: Unable to find javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME_ does not point to the JDK.
It is currently set to "C:\Program Files(x86)\Java\jre1.8.0_20"
Now the frustrating part is that in my environment variables JAVA_HOME does point to the JDK and not the JRE. When I run set JAVA_HOME I get:
JAVA_HOME=C:\Program Files(x86)\Java\jdk1.8.0_20
When I run echo %JAVA_HOME% I get:
C:\Program Files(x86)\Java\jdk1.8.0_20
Here are all of my relevant environment variables (I think):
ANT_HOME: C:\ant
JAVA_HOME: C:\Program Files(x86)\Java\jdk1.8.0_20
PATH: C:\Program Files (x86)\nodejs\;C:\Program Files (x86)\Git\bin;C:\Development\adt-bundle\sdk\platform-tools;C:\Development\adt-bundle\sdk\tools;C:\Program Files (x86)\Java\jre1.8.0_20\bin;C:\Program Files(x86)\Java\jdk1.8.0_20\bin;C:\Users\Casey Ydenberg\AppData\Roaming\npm;C:\ant\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
Any suggestions as to what to try?
Thank you,
Casey
Its probably a misconfigured environmental variable. Your error message indicates ANT is trying to use the JRE.
Go to System Properties > Environment Variables and make sure there is a system variable for JAVA_HOME set to C:\Program Files(x86)\Java\jdk1.8.0_20. Verify that that folder is where your jdk is installed. Open a new command prompt and type echo %JAVA_HOME% to verify.
Based on this answer: Ant Build not able to recognize JAVA_HOME
I tried changing JAVA_HOME to
C:\Progra~2\Java\jdk1.8.0_20
and the JDK part of the PATH to
C:\Progra~2\Java\jdk1.8.0_20\bin.
It worked (yay), but I have absolutely no idea why.
I've written an article about installing Cordova on windows here: http://jewelfarazi.me/installing-cordova-with-android-studio-on-windows/
Your can try this, first add environment variable JAVA_HOME:
C:\Program Files\Java\jdk1.7.0_79
And Then add to environment path:
%JAVA_HOME%\bin
So rather adding the whole path you can do similar for ANT_HOME as well and it should work!

Categories

Resources