9

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$ 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user1034127
  • 349
  • 3
  • 5
  • 14

4 Answers4

9

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.

pradosh nair
  • 913
  • 1
  • 16
  • 28
  • 1
    On macOS hosts, `export JAVA_HOME=$(/usr/libexec/java_home)` is _not_ incorrect, it’s the standard way to set JAVA_HOME on that platform. [java_home(1)](https://web.archive.org/web/20160318211604/https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/java_home.1.html) is not a directory, but a binary that “Returns the path to a Java home directory from the current user's settings.” – Mark G. Jul 17 '20 at 16:55
4

$(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 
jprism
  • 3,239
  • 3
  • 40
  • 56
  • 2
    Actually, /usr/libexec/java_home isn't the java home directory, it's an executable that tries to figure out where the java home directory is and output its path. As the example in the question shows, it outputs "/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home" on OP's computer. So that assignment looks correct to me. – Gordon Davisson Oct 30 '17 at 03:26
3
  • 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
    
jrook
  • 3,459
  • 1
  • 16
  • 33
Arsh_arshu
  • 39
  • 1
2

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.

Capfer
  • 829
  • 9
  • 22