Error: JAVA_HOME is not defined correctly executing maven

前端 未结 14 1460
时光说笑
时光说笑 2020-11-28 05:50

I installed java and set path to environment and when I execute echo $JAVA_HOME I get the following output:

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


        
相关标签:
14条回答
  • 2020-11-28 06:33

    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

    0 讨论(0)
  • 2020-11-28 06:33

    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.

    0 讨论(0)
  • 2020-11-28 06:35

    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
    
    0 讨论(0)
  • 2020-11-28 06:37

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

    0 讨论(0)
  • 2020-11-28 06:38

    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.

    0 讨论(0)
  • 2020-11-28 06:40

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

    export JAVA_HOME=/usr/java/jdk1.8.0_31
    
    0 讨论(0)
提交回复
热议问题