83

I installed java and set the path environment and when I run echo $JAVA_HOME in the terminal I get the following output:

/usr/lib/jvm/java-7-oracle/jre/bin/java

I Also installed apache-maven and changed environment file and now it looks like this:

JAVA_HOME="/usr/lib/jvm/java-7-oracle/jre/bin/java"
M2_HOME=/usr/local/apache-maven/apache-maven-3.0.5
M2=$M2_HOME/bin
MAVEN_OPTS="-Xms256m -Xmx512m"
PATH=$M2:$PATH

But when I execute mvn --version I get a warning:

Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/lib/jvm/java-7-oracle/jre/bin/java/bin/java

Can not find out why it repeats in the end /bin/java/bin/java

2
  • One can use sdkman to manage java and maven tool with ease. Commented Jul 26, 2017 at 5:30
  • In my case, the problem was that JAVA_HOME was defined in the $HOME/.mavenrc and it was pointing to a wrong path. I was able to find out the issue by adding some echo $JAVA_HOME lines to /usr/local/bin/mvn script and running mvn -v
    – hetptis
    Commented Feb 7, 2019 at 9:24

17 Answers 17

110

Assuming you use bash shell and installed Java with the Oracle installer, you could add the following to your .bash_profile

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/jre/bin:$PATH

This would pick the correct JAVA_HOME as defined by the Oracle installer and will set it first in your $PATH making sure it is found.

Also, you don't need to change it later when updating Java.

EDIT

As per the comments:

Making it persistent after a reboot

Just add those lines in the shell configuration file. (Assuming it's bash)

Ex: .bashrc, .bash_profile or .profile (for ubuntu)

Using a custom Java installation

Set JAVA_HOME to the root folder of the custom Java installation path without the $().

Ex: JAVA_HOME=/opt/java/openjdk

5
  • 1
    What if I want to make this persistent between launches? Commented Apr 6, 2016 at 13:09
  • 2
    Worth noting that for ubuntu, you should use .profile instead of .bash_profile according to askubuntu.com/questions/824829/…
    – Pierre
    Commented Apr 4, 2017 at 13:29
  • 1
    What if I want to use java from a share rather than the system java
    – Wearybands
    Commented Aug 7, 2018 at 11:15
  • Just updated the answer as per the comments. Thank you.
    – rbento
    Commented Aug 7, 2018 at 15:42
  • Thanks! Just for curiosity, why "JAVA_HOME=/opt/java/openjdk" works but "JAVA_HOME=$(/usr/libexec/java_home)" doesn't for custom Java installation?
    – zero_yu
    Commented Oct 22, 2018 at 15:46
24

JAVA_HOME should be /usr/lib/jvm/java-7-oracle/jre/.

1
  • 3
    Right, you need to remove the trailing "/bin/java" from your path .. meaning: JAVA_HOME="/usr/lib/jvm/java-7-oracle/jre/"
    – dariusz
    Commented May 25, 2015 at 15:09
23

We open a terminal and look for the location of java:

manuel@zonademanel:~ → whereis java

java: /usr/bin/java /etc/java /usr/bin/X11/java /usr/share/java /usr/share/man/man1/java.1.gz

What we are looking for is /usr/bin/java continue on the command line to find the absolute path , as this is only a symbolic link.

manuel@zonademanel:~ → ls -lah /usr/bin/java

lrwxrwxrwx 1 root root 22 may 19 2015 /usr/bin/java -> /etc/alternatives/java

manuel@zonademanel:~ → ls -lah /etc/alternatives/java

lrwxrwxrwx 1 root root 39 dic 7 11:52 /etc/alternatives/java -> /usr/lib/jvm/java-8-oracle/jre/bin/java

I modified my /etc/environment file with the following values

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/jre/bin" JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"

If I do not want to relogin I can reload the changes with:

source /etc/environment 

​​And run mvn -version correctly

manuel@zonademanel:~ → mvn -version

Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: es_MX, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-70-generic", arch: "amd64", family: "unix"

0
11

$JAVA_HOME should be the directory where java was installed, not one of its parts:

export JAVA_HOME=/usr/lib/jvm/java-7-oracle
9

You might get this error due to couple of reasons. To fix this quickly please follow below steps,

First find the java location. To get a list of your installed Java platforms, run the following command from the terminal:

$ sudo update-alternatives --config java

Now set JAVA_HOME and PATH,

$ export JAVA_HOME=<java_home>

$ export PATH=$JAVA_HOME/jre/bin:$PATH

Create the symlink

$ sudo ln -s <java_home>/jre <java_symlink_path>

When we take your case as a example :

$ sudo ln -s /usr/lib/jvm/java-7-oracle/jre /usr/lib/jvm/java-7-oracle/jre/bin/java

Above command will create the symlink location where the system is trying to find in your issue.

Finally do the

$ mvn --version
1
  • 1
    absolute legend Commented Feb 14, 2022 at 16:03
7

You should set as it is export JAVA_HOME=/usr/java/jdk1.8.0_31 and run with sudo it will execute as shown below.

Example

sudo mvn -v

OUTPUT

Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-70-generic", arch: "amd64", family: "unix"
2
  • This what worked for me on debian! ( manual installation of JDK )
    – timmz
    Commented Apr 9, 2015 at 14:20
  • 2
    sudo solved my problem. My JAVA_HOME was correct, but I was seeing the same error.
    – mmdemirbas
    Commented Apr 19, 2015 at 10:49
3

Firstly, in a development mode, you should use JDK instead of the JRE. Secondly, the JAVA_HOME is where you install Java and where all the others frameworks will search for what they need (JRE,javac,...)

So if you set

JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/bin/java

when you run a "mvn" command, Maven will try to access to the java by adding /bin/java, thinking that the JAVA_HOME is in the root directory of Java installation.

But setting

JAVA_HOME=/usr/lib/jvm/java-7-oracle/

Maven will access add bin/java then it will work just fine.

3

This solution work for me... just type export PATH=$JAVA_HOME/jre/bin:$PATH in the terminal then run mvn -version it will show the same error but with a log like this

which: no javac in (/jre/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin)
Warning: JAVA_HOME environment variable is not set.
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30)
Maven home: /opt/apache-maven-3.2.5
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"

now copy the Java home path i.e. /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre in my case.

now type,

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre

and the error gets resolve. NOTE: paste your own path which is shown by your machine in mvn log at export JAVA_HOME.

2

You must take the whole directory where java is installed, in my case:

export JAVA_HOME=/usr/java/jdk1.8.0_31
2

It happens because of the reason mentioned below :

If you see the mvn script: The code fails here ---

Steps for debugging and fixing:

Step 1: Open the mvn script /Users/Username/apache-maven-3.0.5/bin/mvn (Open with the less command like: less /Users/Username/apache-maven-3.0.5/bin/mvn)

Step 2: Find out the below code in the script:

  if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

Step3: It is happening because JAVACMD variable was not set. So it displays the error.

Note: To Fix it

export JAVACMD=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/java

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/

Key: If you want it to be permanent open emacs .profile

post the commands and press Ctrl-x Ctrl-c ( save-buffers-kill-terminal ).

1

If you are using mac-OS , export JAVA_HOME=/usr/libexec/java_home need to be changed to export JAVA_HOME=$(/usr/libexec/java_home) . Steps to do this :

 $ vim .bash_profile

 export JAVA_HOME=$(/usr/libexec/java_home)

 $ source .bash_profile

where /usr/libexec/java_home is the path of your jvm

1

Use these two commands (for Java 8):

sudo update-java-alternatives --set java-8-oracle
java -XshowSettings 2>&1 | grep -e 'java.home' | awk '{print "JAVA_HOME="$3}' | sed "s/\/jre//g" >> /etc/environment
1

In my case, the problem was in vscode.

I use windows and installed Ubuntu on my computer, so vscode terminal uses the Ubuntu. I just go to settings.json and change to cmd.exe again

1

I had this same issue but with open jdk and none of the answers here helped. The trouble was that the mvn script was appending /bin/java at the end of JAVA home while trying to run java commands.

The solution for me was to manually edit the /usr/local/apache-maven/apache-maven-3.3.9/bin/mvn script (your own script might be installed differently; just run which mvn) and change

   if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java" 
    fi
  else
    JAVACMD="`which java`"
  fi
 fi

To

if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else 
      JAVACMD="$JAVA_HOME"
    fi
  else
    JAVACMD="`which java`"
  fi
fi
0

add in .bash_profile

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/jre/bin:$PATH

and then

source .bash_profile

0

I had similar issue and it was not getting resolved even after editing and sourcing the .bash_profile, .bashrc file. I got it resolved by editing the maven.sh file directly.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.amzn2.0.1.x86_64
export M2_HOME=/opt/maven
export M2=/opt/maven/bin
export PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2HOME:$M2

and then sourcing the file

source /etc/profile.d/maven.sh

For those who didnt get they issue resolved with other answers, give this a try as well.

0

If you don't know the java installation path, installations usually create a symlink. Using this symlink that doesn't change regardless of the version, you can paste this line into your .bashrc:

# .bashrc
export JAVA_HOME="$(realpath /etc/alternatives/java | sed -E 's/(.*)((\/[^/]+){2,})/\1/g')"

The realpath function returns the absolute path of the symlink. Then the sed function does a replace with regex, removing two parts from the end.

I haven't tested what I'm going to say next, it's a guess... It can be useful if you use several versions, because if you change the version with sudo update-alternatives --config java, the reference already points to the current version, IF the symlink is changed automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.