Maven Java home configuration - java

I installed JDK and set up Maven. Call of mvn -version i get returns:
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_HOME variable is set to C:\Program Files\Java\jdk1.8.0_131\bin in system variables.
Call of %JAVA_HOME% returns path C:\Program Files\Java\jdk1.8.0_131\bin.
Where is the problem?

As you can see in the documentation the JAVA_HOME variable must point to the java installation path, not to the bin folder.
Change it to C:\Program Files\Java\jdk1.8.0_131

The question is about Windows but I came here trying to solve the problem on Ubuntu.
I faced a similar problem. I configured $JAVA_HOME in /etc/environment
like $JAVA_HOME=PATH_TO_JDK for example $JAVA_HOME=/home/max/jdk1.8.0_144
Careful with
White space after path declaration $JAVA_HOME=/home/max/jdk1.8.0_144[[_NO_WHITE_SPACE_AFTER_DECLARATION]]
Don't put any double apostrophe $JAVA_HOME="/home/max/jdk1.8.0_144"
Don't put /bin e.g $JAVA_HOME=/home/max/jdk1.8.0_144/bin <- This is wrong

Yes, original question is about pure windows, but for those who came here wondering about windows linux subsystem WSL, I stumbled across the thing trying to set up my win WSL, to use windows java Open jdk binaries. Though in a while i gave up on that idea. Installed jdk with 'sudo apt install ...' and then set WSL java home from installed path:
root#mypc://# java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0+deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
root#mypc://# which java
/usr/bin/java
root#mypc://# realpath /usr/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
use yours realpath instead.
root#mypc://# export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
root#mypc://# mvn -v
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T15:06:16Z)
Maven home: /mnt/c/javaDir/mvn/apache-maven-3.6.2
Java version: 1.8.0_265, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-43-microsoft", arch: "amd64", family: "unix"
add export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 to a ~/.bash_profile for this being set every time linux subsystem is lunched.

Related

Warning: JAVA_HOME environment variable is not set

My OS is Windows 10. I had an error when starting the .\artemis in the Windows PowerShell. It said:
Warning: JAVA_HOME environment variable is not set.
I already setting the JAVA_HOME's path at C:\Program Files\Java\jdk-19\bin. This is the result for echo %JAVA_HOME%:
C:\Program Files\Java\jdk-19\
I tried to test the mvn -v:
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: C:\apache-maven-3.8.1\bin\..
Java version: 19.0.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-19
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
(it should not be shown if the JAVA_HOME is not set tho)
I tried the java -version:
java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)
The javac -version:
javac 19.0.1
Can someone explain how to fix it?
I forgot to mention it
when I use git bash to run ./artemis the result is
./artemis: line 93: C:\Program Files\Java\jdk-19;/bin/java: No such file or directory
Install and set Java SDK 11 as JAVA_HOME.
Most applications will not work with SDK 18 or higher.
Artemis notes:
Note on Java versions: The old v17.0.1 version of the Artemis software required Java version 1.8 to run. All recent releases from v18.0.0 onwards require a minimum of Java 9 and ideally Java 11. This must be installed first.
Extracted from http://sanger-pathogens.github.io/Artemis/
Did you try using the short name? It’s been some time since I used Windows but
if I recall correctly I used to use PROGRA~1 instead of "Program files".

How to fix the maven error The JAVA_HOME environment variable is not defined correctly

When I check on maven's version on a virtual environment, I get the following errors:
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
However, I am able to print maven's version outside of virtual environment
mvn -version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T00:58:13-07:00)
Maven home: /usr/local/Cellar/maven/3.5.2/libexec
Java version: 1.8.0_152, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"
I have used set in ~/.bash_profile
set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk
set PATH=$JAVA_HOME/jre/bin:$PATH
I don't understand the error as JAVA_HOME is pointing to a JDK and not a JRE? Thanks for answering!
This is not correct:
set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk
Notice that the output of mvn tells you the location of Java home is /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre.
So the location of the JDK home is probably /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home.
Secondly, you need to export the variable, not just set it.
Write like this:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home
After you update your ~/.bash_profile,
to test that the setup is correct,
start a new shell, and check the output of echo $JAVA_HOME.

maven ignoring jenv settings

I had OSX with Java 1.6 installed, and I just installed jenv along with Java 1.7:
$ jenv local '1.7'
$ jenv versions
system
1.6
1.6.0.65
* 1.7 (set by /Users/me/workspace/.java-version)
1.7.0.79
oracle64-1.6.0.65
oracle64-1.7.0.79
jenv is doing its job, with $ java -version always working, showing 1.6 when I've set it to 1.6, and 1.7 when I've set it to 1.7:
$ java -version
java version "1.7.0_79" <--------------------------------- YAY!!
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
... but maven is ignoring my settings:
$ mvn -version
Apache Maven 3.0.2 (r1056850; 2011-01-08 19:58:10-0500)
Java version: 1.6.0_65, vendor: Apple Inc. <--------------------------------- BAH!!
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"
I found this SO question where the guy just needed to hardcode his JAVA_HOME inside .mavenrc, but I don't want mine hard-coded (thus jenv!), and I don't have a ~/.mavenrc, nor an /etc/mavenrc.
The version it's using seems to be from whatever's first on the /usr/libexec/java_home output, so in the short term I was able to get 1.6 back again by tweaking 1.7's Info.plist file (from this SO post), but that just means I get 1.6 instead of 1.7.
Any ideas?
You need to install the jenv maven plugin, try the following command and reload your shell:
jenv enable-plugin maven
I had a similar problem. I got things running by prefixing all command with jenv exec:
jenv exec mvn -version
Now, there's a new option how to set JAVA_HOME via jenv export plugin:
jenv enable-plugin export
See https://github.com/gcuisinier/jenv/issues/44#issuecomment-233124185
If you have enabled the maven and export plugins, and still have problems, check that you don't have a ~/.mavenrc file which is setting JAVA_HOME.
If jenv enable-plugin export still doesn't work then you should restart the Terminal and try again

Properly installing java 8 along with java 7

I've JDK 1.7 installed on my windows 7 machine and after installing JDK 1.8 u20 I'm having following error:
C:\>java -version
Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'
has value '1.8', but '1.7' is required.
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.
My PATH variable points to the older version (i.e. 1.7).
What is wrong here and how I could use java 8 along with java 7?
The problem is that Java 8 installs a lot of stuff that you don't really need:
\windows\system32 contains Java 8 java.exe, javaw.exe and javaws.exe. Your path probably has system32 near the beginning, so these tend to be run by default.
The system path variable starts with C:\programdata\Oracle\Java\javapath. This folder contains java.exe, javaw.exe and javaws.exe as symlinks to the JRE 8 executables.
I've deleted the system32 files and removed C:\programdata\Oracle\Java\javapath from the system path. This seems to cure the problem. I can now switch versions by pointing JAVA_HOME and PATH to the appropriate folders.
Oracle, it seems, are determined to make it hard to run multiple versions. This is understandable with the JRE, but it's crazy with JDKs, as developers almost always need multiple versions of Java.
EDIT: I find this batch script is useful for switching JDKs. Usage: jdk.bat 6|7|8. You might have to edit the installation location for Java.
#echo off
if "%1"=="" goto report
set _version=%1
shift
if "%1"=="DBG" shift & echo on
set _command=%1 %2 %3 %4 %5
set _jdkdir=
set _jdkver=
for /D %%f in ("C:\Program Files\java\"jdk1.%_version%.*) do call :found "%%f"
if "%_jdkdir%"=="" goto notfound
set java_home=C:\Program Files\java\%_jdkdir%
call :javapath
path %new_path%
goto :report
:javapath
setlocal enabledelayedexpansion
set _jdirs=
for /D %%j in ("C:\Program Files\java\*") do set _jdirs=!_jdirs!#%%~fj\bin
set _jdirs=%_jdirs%#
set _javabin=%java_home%\bin
set _fpath="%PATH:;=" "%"
call :checkpath %_fpath%
endlocal & set new_path=%_javabin%
goto :eof
:checkpath
if _%1==_ goto :eof
echo %_jdirs% | find /i "#%~1#" 1>nul 2>&1
set _err=%errorlevel%
if not %_err%==0 set _javabin=%_javabin%;%~1
if %_err%==0 echo Removed %~1 from path
shift
goto :checkpath
:report
javac -version
%_command%
goto :eof
:notfound
echo No JDK matching [C:\Program Files\java\jdk1.%_version%.*] found.
goto :eof
:found
set _jdkdir=%~n1%~x1
for /F "tokens=2,3 delims=." %%a in ("%_jdkdir%") do set _jdkver=1.%%a.%%b
goto :eof
In the START menu type "regedit" to open the Registry editor
Go to "HKEY_LOCAL_MACHINE" on the left-hand side registry explorer/tree menu
Click "SOFTWARE" within the "HKEY_LOCAL_MACHINE" registries
Click "JavaSoft" within the "SOFTWARE" registries
Click "Java Runtime Environment" within the "JavaSoft" list of registries
here you can see different versions of installed java
Click "Java Runtime Environment"- On right hand side you will get 4-5 rows . Please select "CurrentVersion" and right Click( select modify option)
Change version to "1.7"
Now the magic has been completed
You can't have your cake and eat it too. :)
When you set your PATH variable to JDK 1.8, The problem should resolve. You can run your programs in JDK 1.7 by setting PATH manually using set PATH from command prompt or can go to the JDK 1.7 directory and run your program from there.
But there can be only one JDK in your PATH.
I had the same problem then realized my program was running out of c:\Windows\SysWOW64 and hence running the old java.exe. Once I stopped running out of that directory (which contains the Java 7 exe), the problem went away since it followed the path properly to java8.
If you are not comfortable to remove any files in Windows manually, just put your JAVA_HOME path in the front of Windows dirs.
Define JAVA_HOME environment variable in Windows 7 and use it in variable PATH on first position of Path variable.
JAVA_HOME -> D:\dev\Java\jdk1.8.0_45
Path -> %JAVA_HOME%\bin;%SystemRoot%\system32;
You could define a java7 alias that would lead to the bin folder of your JDK7, and then change your environment variable so that it points JDK8. The default JDK would then be JDK8.
Thanks #Chris, for me it was solved just deleting the files from C:\Windows\System32.
I got this problem when using the Sencha Command, but of course it has nothing to do with Sencha. It is a Java installation issue.
Apparantely this issue was also really annoying for people with Java 1.6 and 1.7 according to this post:
Registry Key '...' has value '1.7', but '1.6' is required. Java 1.7 is Installed and the Registry is Pointing to it
In my case, I uninstalled other versions of Java keeping Newer version
I had Java 7 and Java 8 in the system.
I Uninstalled 7 and kept 8, in the path, I already had Java 8.
It worked for me :)
This issue is annoying when dealing with multiple JDKs for developing on Windows 10 (I couldn't make use of the OS system path change as suggested here).
As a partial answer (since this might not be intended as 'properly' installed) I'm doing quite fine using Cygwin to switch JAVA_HOME and run Maven builds with different JDKs (1.7,1.8) installed (via Oracle installers).
So if you have Cygwin installed (or can install and use it) and willing to use JAVA_HOME env variable (useful with Maven as in this example below) you could do like this (BUT be aware of the 'trick' in the answer at link 1 and adjust your local paths accordingly):
export JAVA_HOME=/cygdrive/c/Progra~1/Java/jdk1.8.0_74
(mvn --version output)
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5;
2015-11-10T17:41:47+01:00) Maven home:
C:\Work2\bin\apache-maven-3.3.9-bin\apache-maven-3.3.9 Java version:
1.8.0_74, vendor: Oracle Corporation Java home: C:\Progra~1\Java\jdk1.8.0_74\jre Default locale: it_IT, platform
encoding: Cp1252 OS name: "windows 10", version: "10.0", arch:
"amd64", family: "dos"
export JAVA_HOME=/cygdrive/c/Work2/bin/Java/jdk1.7.0_79_64bit/
(mvn --version output)
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5;
2015-11-10T17:41:47+01:00) Maven home:
C:\Work2\bin\apache-maven-3.3.9-bin\apache-maven-3.3.9 Java version:
1.7.0_79, vendor: Oracle Corporation Java home: C:\Work2\bin\Java\jdk1.7.0_79_64bit\jre Default locale: it_IT,
platform encoding: Cp1252 OS name: "windows 8.1", version: "6.3",
arch: "amd64", family: "windows"
https://stackoverflow.com/questions/14567191/export-java-home-with-spaces-in-cygwin
Edit this text in base your own values, save as ".reg", execute, enjoy :)
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.8.0_74]
"JavaHome"="C:\\Program Files\\Java\\jre8"

Detected JDK Version: 1.6.0-24 is not in the allowed range 1.7

When I ran the command
mvn clean package
I am getting error:
Detected JDK Version: 1.6.0-24 is not in the allowed range 1.7.
How to fix the above error? I tried to check jdk version isntalled and got this
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode)
how to fix this?
Normally when execute the mvn -v you may see something like
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da;
2013-02-19 20:51:28+0700)
Maven home: C:\Java.Application\Apache\apache-maven-3.0.5\bin\..
Java version: 1.7.0_15, vendor: Oracle Corporation
Java home: C:\Java.Application\Sun\Java\jdk1.7.0_15\jre
Default locale: en_US, platform encoding: MS874
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
If the result point to the JDK Version: 1.6.0-24 or other than your expect. Please simply set the JAVA_HOME to your JDK Version: 1.7.0_03 instead.
I hope this may help.
Make sure that your environment (PATH) is set up to use the JDK version that you want to use for compiling. You can check this by running the following in the shell or command window where you want to run Maven:
java -version
javac -version
Make sure that both java and javac are pointing to the correct version. It's possible that you have a 1.7 JRE (which does not include the compiler) and a 1.6 JDK (which includes the compiler) and the wrong one is being used.
If necessary, adjust your JAVA_HOME and PATH environment variables.
Just noticed that you're on CentOS - it's possible that java and javac point to different versions. Use the alternatives command to check that, see here for an example: http://wiki.centos.org/HowTos/JavaRuntimeEnvironment - you will have to check both the java and the javac command.
Check your JRE System Library in Eclipse. Does it show jdk1.6.0-24?
For me I had to right click the JRE System Library and go to Build Path -> Configure Build Path, in the Libraries tab I had to click JRE System Library and edit it. I set it to the Workspace Default JRE, which for me is jdk1.7.0_80 (not sure why it wasn't set to that already).

Categories