[Solved] NB: JAVA_HOME should point to a JDK not a JRE

NB: JAVA_HOME should point to a JDK not a JRE

Before deploying the project on Centos 6.10, I installed jdk using yum, configured environment variables, java -version output normally, java -jar xx.jar can run Spring Boot project normally. But after installing Maven and configuring it an error is reported.

Error 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

Solution
In the Maven->bin directory, Linux series servers and Macs modify the mvn file by adding the local jdk directory to the following location, with the quotes copied from the document.

On Windows, modify mvn.bat to add the same location: set JAVA_HOME=D:\Java\jdk1.8.0_101

Here are the details of the process
The initial /etc/profile configuration is as follows.

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar、
export PATH=$PATH:$JAVA_HOME/bin

After checking the directory, I found no bin directory. I thought there was something wrong with the yum installation, and then I changed to the unpacked version and deleted all Jdk related files.

Jdk and Maven are both downloaded from the official website and unpacked to the server.
The paths are: /root/soft/jdk1.8.0_221 and /root/soft/apache-maven-3.5.4

JAVA_HOME and CLASSPATH have been configured in /etc/profile as follows.

JAVA_HOME=/root/soft/jdk1.8.0_221
JRE_HOME=/root/soft/jdk1.8.0_221/jre
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME CLASSPATH

M2_HOME=/root/soft/apache-maven-3.5.4
PATH=$M2_HOME/bin:$PATH
export M2_HOME PATH

 

Run java -version Normal output.

java version “1.8.0_221”
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

 

Running mvn -version still outputs the same error as at the beginning, echo $PATH and which java output are normal. I’ve been tossing and turning for hours for several days, searching Google, but still no solution.
After solving the maven error JAVA_HOME should point to a JDK not a JRE
blog to find ideas.
In the bin directory of the Maven folder, look at the startup script (named mvn file on Unix, mvn.cmd on Windows), using mvn as an example.

if $cygwin ; then
[ -n “$MAVEN_HOME” ] &&
MAVEN_HOME=`cygpath –unix “$MAVEN_HOME”`
[ -n “$JAVA_HOME” ] &&
JAVA_HOME=`cygpath –unix “$JAVA_HOME”`
[ -n “$CLASSPATH” ] &&
CLASSPATH=`cygpath –path –unix “$CLASSPATH”`
fi

# For MinGW, ensure paths are in Unix format before anything is touched
if $mingw ; then
[ -n “$MAVEN_HOME” ] &&
MAVEN_HOME=`(cd “$MAVEN_HOME”; pwd)`
[ -n “$JAVA_HOME” ] &&
JAVA_HOME=`(cd “$JAVA_HOME”; pwd)`
# TODO classpath?
fi
JAVA_HOME=`/root/soft/jdk1.8.0_221` # add
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

The script is to determine whether the system variable exists, it should be the place where the problem is, follow the instructions in the blog post plus the location of jdk. Get it done !!!!

Similar Posts: