I’m super new to CS and very unfamiliar with UNIX/Bash vocab.
I’m currently trying to install Maven, and I made a few mistakes by copy pasting old installation instructions (with incorrect version numbers) and now the whole process is so messed up. I first tried to follow the given instructions on the website, that didn’t work, then I tried a few other ones, and now I’m worried that I’m left with a mess by following different sets of instructions.
I’ve reached a point now where if I type in mvn -version
I get the following:
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
Originally, my JAVA_HOME was set to jdk-install-dir, which still gave me the above error message. I tried redownloading the jdk (version 13.0.1) from Oracle, dragged that file to my home directory, and unzipped it. Then I set my JAVA_HOME to that unzipped file, jdk-13.0.1.jdk, and updated my PATH variable. After all this, I’m still getting this same error message, and I’m not sure what to do.
For the reference, here are what some relevant environment variables are set to (I didn’t include irrelevant info from PATH):
~ echo $JAVA_HOME jdk-13.0.1.jdk ~ echo $PATH /usr/local/apache-maven/apache-maven-3.6.3/bin:/opt/apache-maven-3.6.3/bin:jdk-install-dir/bin:/usr/local/apache-maven/apache-maven-3.3.9/bin:/opt/apache-maven-3.6.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/allyson/apache-maven-3.6.3/bin:/Users/allyson/apache-maven-3.6.3/bin:jdk-12.0.1.jdk/bin:/Users/allyson/apache-maven-3.6.3/bin:JDK-13.0.1.jdk/bin ~ echo $M2 /usr/local/apache-maven/apache-maven-3.6.3/bin ~ echo $M2_HOME /Users/allyson/apache-maven-3.6.3
One thing I’m curious about: for the M2 and M2_HOME variables, is it supposed to be that one is in my home directory (/Users/allyson
) and one is for /usr/local
?
Advertisement
Answer
OK, take a deep breath, and we’ll walk through this. Each of these environment variables has a purpose, and once you understand what those purposes are, this makes a lot more sense. Mixing tutorials is not necessarily a problem, but you’ll want to understand what you’re doing, rather than just blindly copy values from the internet.
JAVA_HOME
is intended to identify to the system environment where to find a java runtime environment. It needs to be set to the full path of where your JDK has been installed. On windows, this might beC:Program FilesJavajdk-13.0.1
. On a Linux system, you have a bit more flexibility. Common locations might be/opt/java/jdk-13.0.1
or/usr/local/java/jdk-13.0.1
. If you installed your JDK somewhere else, then you need to use that path instead. The messageNB: JAVA_HOME should point to a JDK not a JRE
refers to a common mistake when installing maven — maven requires a JDK, not a plain JRE. This error is so common that any timeJAVA_HOME
points to a folder that isn’t a JDK, it prints this warning (even if the folder in question isn’t actually a JRE).M2_HOME
is supposed to be set to the full path where maven is installed (i.e. the place where you unzipped it). This more or less helps maven “find itself” if it should need to for whatever reason. Strictly speaking, this one isn’t necessary. (It’s not set on my system, and maven works fine for me). It’s mostly a convenience for setting the next environment variable.M2
is the full path to the folder where the maven executable is. This will almost always be$M2_HOME/bin
, but it’s certainly possible to do weird things, and this will let you work around those situations. Obviously, this won’t work if you didn’t specify$M2_HOME
. This one isn’t strictly necessary, either, and is mostly a convenient way of setting up the next one.PATH
is where your Linux system looks to find programs to run when you type their name on the command line. For ease of use, you’ll want to make sure that the maven and java executables are included somewhere in the:
delimited list. Most Linux distributions already have a defaultPATH
set up for you in a shell resource file of some kind. You’ll want to refer to their documentation for how to add another entry to the path, but a common idiom would bePATH=$PATH:$M2
(which would append the value of$M2
to the value of$PATH
and then store the result back intoPATH
. If you didn’t set up$M2
or$M2_HOME
, you’ll need to do something else.
So, TL;DR, if you installed your JDK in /opt/java/jdk-13.0.1
and unzipped maven into /opt/maven/apache-maven-3.6.3
, your bear minimum working values are:
export JAVA_HOME=/opt/java/jdk-13.0.1 export PATH=$PATH:$JAVA_HOME/bin:/opt/maven/apache-maven-3.6.3/bin
And if you wanted a complete set
export JAVA_HOME=/opt/java/jdk-13.0.1 export M2_HOME=/opt/maven/apache-maven-3.6.3 export M2=$M2_HOME/bin export PATH=$PATH:$JAVA_HOME/bin:$M2
It’s worth noting that most Java IDEs will include a GUI for setting up maven and Java within the IDE (the settings will typically only work within that IDE). It’s often much easier for beginners to get up and running that way.