Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»(Resolved) JAVA_HOME environment variable is not defined correctly on macOS

    (Resolved) JAVA_HOME environment variable is not defined correctly on macOS

    By RahulSeptember 15, 20222 Mins Read

    JAVA_HOME is an environment variable used by various Java-based applications to locate JVM (JDK) on any system. Similarly, Maven also required the JAVA_HOME to be set correctly on your system.

    Advertisement

    The Problem:

    When I tried to check the Maven version on our macOS PC, I got the following error in the output.

    mvn --version 
    
    The JAVA_HOME environment variable is not defined correctly,
    this environment variable is needed to run this program.
    
     JAVA_HOME environment variable is not defined correctly
    Error: JAVA_HOME environment variable is not defined correctly

    The Solution:

    As the error message clearly shows that the JAVA_HOME environment variable is not set on my PC. So the simple solution is to set it correctly. Use the below steps to resolve this issue.

    The macOS 10.5 (Catalina) and later version use the Zsh as the default shell. The older version uses the Bash shell. So first is to identify the shell that is running on your system. Open a terminal and type:

    echo $SHELL 
    

    For Zsh the output will be /bin/zsh and for the Bash shell the output will be /bin/bash.

    Now, set the JAVA_HOME environment variable based on the active shell:

    • Zsh (/bin/zsh): Edit the ~/.zshenv file and append the below code.
      ~/.zshenv
      export JAVA_HOME=$(/usr/libexec/java_home)
    • Bash (/bin/bash): Edit the ~/.bash_profile file and append the below code.
      ~/.bash_profile
      export JAVA_HOME=$(/usr/libexec/java_home)

    This should resolve the issue on your system. To confirm, open a new shell and print the JAVA_HOME environment variable value and also confirm with the command you were getting the error.

    echo $JAVA_HOME 
    mvn --version 
    
    Error: JAVA_HOME is not defined correctly
    Resolved: JAVA_HOME is not defined correctly

    Note: If the issue is still not resolved after setting the environment variable correctly, make sure other scripts are not overriding it. You can find the script order of execution here.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    sleep Command in Linux with Examples

    20 Basic Linux Commands for the Beginners (Recommended)

    tail Command in Linux with Examples

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    • 20 Basic Linux Commands for the Beginners (Recommended)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.