Mac echo $JAVA_HOME return blank

15,534

Solution 1

Statement export JAVA_HOME=$(/usr/libexec/java_home) seems correct; bash would have evaluated the information within $() and returned a blank if .

Looks like a syntax error caused the error not to execute, leaving JAVA_HOME empty.

The usage of $() is discussed in this link. Difference between ${} and $() in Bash.

Solution 2

$(str) where str should be a command. In your case, $(/usr/libexec/java_home) will return nothing. Just use

export JAVA_HOME=/usr/libexec/java_home 

Solution 3

I have just run into this issue after installing Amazon Corretto jdk15. But, in my case, I just closed and reopened the terminal and everything worked, that is, by running

echo $JAVA_HOME

It prints out to the console:

/Users/username/.sdkman/candidates/java/current.

Solution 4

  • Create a .zshrc file on your terminal

    touch ~/.zshrc
    
  • now click go -> home -> press shift+command+.

  • You can now see the hidden files. Open .zshrc file and type:

    export JAVA_HOME=$(/usr/libexec/java_home)
    export PATH=/opt/homebrew/bin:$PATH
    
Share:
15,534
user1034127
Author by

user1034127

Updated on August 08, 2022

Comments

  • user1034127
    user1034127 over 1 year

    I don't know why echo $JAVA_HOME return blank

    Abdelmajids-iMac:~ majid$ vi .profile

    export PATH=/usr/local/bin:(...)
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load 
    RVM into a shell session *as a function*
    export JAVA_HOME=$(/usr/libexec/java_home)
    ~                                                                                                    
    
    ~      
    
    
    Abdelmajids-iMac:~ majid$ /usr/libexec/java_home
    /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
    Abdelmajids-iMac:~ majid$ vi .bash_profile
    Abdelmajids-iMac:~ majid$ source .bash_profile
    -bash: .bash_profile: line 1: syntax error near unexpected token `('
    -bash: .bash_profile: line 1: `export PATH=/usr/local/bin:(...)'
    Abdelmajids-iMac:~ majid$ echo  $JAVA_HOME
    
    Abdelmajids-iMac:~ majid$