JAVA_HOME is not working in maven - maven

java is installed at this path
$ which java
/usr/bin/java
mvn -version is giving this error
$ 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
I have tried some of the solutions that were available online, but those don't work for me. Some of those solutions suggested adding
$export JAVA_HOME = /usr/libexec/java_home
or
$export JAVA_HOME = $(/usr/libexec/java_home)
to below files
~/.bashrc
~/.bash_profile
~/.profile
Also when I try to execute one shell command, it shows me error like
Error: JAVA_HOME is not defined correctly.
CARBON cannot execute /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java

After struggling for almost a day, I found out that maven is not reading the $JAVA_HOME from either of
~/.bashrc
~/.bash_profile
~/.profile
but it reads $JAVA_HOME from ~/.mavenrc
So finally, when I added
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home
in ~/.mavenrc then got output
mvn -v
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T15:39:06-04:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_141, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre
Default locale: en_CA, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"

To fixed it, update the JAVA_HOME like following :
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
Run Maven again :
$ mvn -version

maven reads JAVA_HOME from ~/.mavenrc. Add path for JAVA_HOME to ~./mavenrc and source it.
$ vim ~/.mavenrc
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .mavenrc

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 quotes, e.g. 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

Please check once there should be no ';' in JAVA_HOME value, if there is semicolon then it would show above error.

I had
JAVA_HOME
set but did not export it and maven was not picking up the correct JAVA_HOME.
Once I exported JAVA_HOME then it got resolved. Do not forget to export JAVA_HOME so that the variable is available to child processes (UNIX* like OS)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home

Related

Maven cannot identify JAVA_HOME on macOS 11.6.7

I am on macOS 11.6.7. My JAVA_HOME variable is set to ~/.zshenv as:
export JAVA_HOME=$(/usr/libexec/java_home)
My terminal output for both echo $JAVA_HOME and mvn --version are as follows:
<redacted> ~ % echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-18.0.2.jdk/Contents/Home
<redacted>~ % mvn --version
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.
I have attempted to source ~/.zshenv as well as restarting shell. The JAVA_HOME variable is read correctly even still. What am I missing? Are there other configuration files I have to check/set for Maven specifically?

Set environment variable path for maven (mvn) in mac for Catolina or higher version

When I am trying to set environment variables for mvn in MAC, I am able to set for one session but if I open another terminal and try mvn -version, it doesn't work.
Commands I followed:
vi $HOME/.z_profile
--Added this path in .z_profile file
export M2_HOME=/Users/namangupta/Downloads/apache-maven-3.6.3
export PATH=$PATH:$M2_HOME/bin/
source .z_profile
mvn -version
--Output:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/namangupta/Downloads/apache-maven-3.6.3
Java version: 15, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"
After these steps when I open another terminal and try mvn -version I get
zsh: mvn cmd not found and on running printenv the path gets reset. Can anyone please tell me where I am wrong in setting the path?
For MacOS Catolina or higher version, since you have zsh instead of bash, you will need to change the path to another file instead of .bash_profile (used for previous versions) as shown below:
--to open the file
nano ~/.zshenv
--paste the following lines and append path for anything else if you want to, just ensure you enter the proper version u have
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home"
export M2_HOME="/Users/namangupta/Workspace/setup_files/mvn/apache-maven-3.8.1"
export PATH=$PATH:$M2_HOME/bin:$JAVA_HOME/bin
--save the file
Ctrl+x, then 'Y' to save and then Enter
--to save the environment variables path
source .zshenv
And you are good to go. To check you can try printenv to check or mvn -version or java -version or echo $PATH
For MacOS Monterey (version 12.5)
-> open ~/.zshenv
->Add following to .zshenv File
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home"
export M2_HOME="/Users/namangupta/Workspace/setup_files/mvn/apache-maven-3.8.1"
export PATH=$PATH:$M2_HOME/bin:$JAVA_HOME/bin
->Save file
->mvn -version
This solution worked for me.Thank you :)
You had everything right except the filename - it should be $HOME/.zprofile (with no underscore). That's why it worked when you manually sourced from .z_profile, but didn't work when starting a new terminal.

macbook 10.15.5 install maven failed: NB: JAVA_HOME should point to a JDK not a JRE

I know there are a lot of questions/answers about this question, but all were failed in my case.
I installed jdk 1.8 and then installed maven3.6.3, java runs well but mvn command didn't work, see my command output and env:
java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
mvn -v
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
mac OS version: 10.15.5
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
echo $PATH
/usr/local/apache-maven-3.6.3/bin:/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:.
config in ~/.bash_profile
export M2_HOME=/usr/local/apache-maven-3.6.3
PATH=$M2_HOME/bin:$JAVA_HOME/bin:$PATH:.
#Mac OSX 10.15.5 or later version need this configure
export JAVA_HOME=$(/usr/libexec/java_home)
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export PATH
I also source ~./bash_profile to make configure effective.
I would suggest revert all the changes you have made in ~/.bash_profile. Checkout my way to make the maven work:
Download latest maven zip package apache-maven-<version>-bin.zip from Apache maven download page.
Unzip it to any place, e.g: ~/Develop/apache-maven-3.6.0
Create a link with command sudo ln -sf ~/Develop/apache-maven-3.6.0/bin/mvn /usr/local/bin/mvn (change version number per your case, if the local bin folder missing, just create it mkdir -p /usr/local/bin).
Check the installation mvn --version
Hope it works for you.
After hundreds updating configure file, now mvn -version can works, share my update and hope it can help you.
fill or update vim ~/.mavenrc(in the beginning, this file isn't exist)
# my MacOS is 10.15+
export Java_HOME=$(/usr/libexec/Java_home)
and then source ~/.mavenrc to let system env effective.
Summary:for my mac system, seems maven couldn't get JAVA_HOME from .bash_profile or .profile, so if you confirm your JAVA_HOME is set correctly and MAVEN_HOME configured, then you can have a try in this way.

Terminal is not sourcing .bash_profile, but sourcing .profile

I created a .bash_profile and added the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
export M2_HOME=/Users/aastha/apache-maven-3.3.9
export PATH=$PATH:$M2_HOME/bin
When I check echo $PATH, I get the following
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
(i.e) maven path is not imported.
and also mvn -version shows: mvn:command not found
When I manually source using source ~/.bash_profile and then do echo $PATH, I am able to see correct mvn path that I have entered in PATH variable in .bash_profile.
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/aastha/apache-maven-3.3.9/bin
mvn -version shows:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: /Users/aastha/apache-maven-3.3.9
Java version: 1.8.0_101, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.5", arch: "x86_64", family: "mac"
My terminal does not source .bash_profile until I manually source it using
source ~/.bash_profile
Note: I created a .profile with the similar content and found out in this case mvn -version shows the maven is installed. And terminal is sourcing .profile automatically.
Why is my terminal not able to source .bash_profile automatically
Could somebody please help?
Thanks in advance :D

Permanent Maven Installation

How can I make the maven installation permanent? After downloading the apache-maven to /usr/local, I need to print the series of "export" command lines every single time I want to use it. Even after I got the maven to work, maven doesn't work when I restart the terminal.
Added this to ~/.bash_profile:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1/
export PATH=$PATH:$M2_HOME/bin
Command-line:
$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1/
$ export M2=$M2_HOME/bin
$ export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/home
$ export PATH=$M2:$JAVA_HOME/bin:$PATH
$ mvn –-version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T12:37:52-05:00)
Maven home: /usr/local/apache-maven/apache-maven-3.2.1
Java version: 1.6.0_65, vendor: Apple Inc.
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.9.3", arch: "x86_64", family: "mac"
After restarting command-line:
$ mvn –-version
-bash: mvn: command not found
Instead of adding export PATH=$PATH:$M2_HOME/bin to ~/.bash_profile, I also tried adding the other export command-lines that sets M2, JAVA_HOME and PATH. This doesn't work.
After moving the application directory to /usr/local
Make a soft link in /usr/bin for universal access of mvn
sudo ln -s /usr/local/apache-maven-X.X.X/bin/mvn /usr/bin/mvn
Verify mvn installation
mvn --version
Since you are using Mac, I recommend that you install Homebrew (a package manager for OS X). Given that, you can install Maven using the brew command, e.g.
$ brew install maven
You need to make /etc/profile.d/maven.sh with the following content:
export M2_HOME=/opt/apache-maven-3.2.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
Mac OSX
I found two neat tutorials:
1) mkyong
2) journaldev
Win7 You need to set up environment variables. In Win 7, in Start menu you can type Edit the system environment variables, then click Environment variables. In System variables, add 2 New variables:
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-2.2.1 (this one is custom)
M2=%M2_HOME%\bin
After that, among the list of variables, find the one called Path, click Edit, and in the end, after this ; add %M2%;
If you did everything right, and if you have JAVA_HOME set up, then Maven should be permanent.
Do you try putting it in a .profile file instead of .bash_profile?
See What's the difference between .bashrc, .bash_profile, and .environment?
.profile is handled at login, so you need to logout and login for changes to become active.
In the Mac terminal, type below to open text editor
touch ~/.bash_profile
open ~/.bash_profile
Then in the editor, type:
export MAVEN_HOME=~/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin
Close the editor and the contents will be saved. If you check the maven version, you will get response:
nisha$ mvn -version
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/nisha/apache-maven-3.8.1
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.4", arch: "x86_64", family: "Mac"
Now, even if you close and open the terminal, you will be able to get the above output when you hit mvn -version

Resources