Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java_HOME not found when changed shell from Bash to Zsh on OSX?

Tags:

bash

macos

zsh

This is weird, I have set JAVA_HOME for my mac which can be found when I am using bash shell, but if I change shell, I get a message saying JAVA_HOME not set. What could be going on here?

like image 897
piyushdubey Avatar asked May 29 '15 19:05

piyushdubey


People also ask

Where is JAVA_HOME in Mac?

What is /usr/libexec/java_home. 2.1 On Mac OS X 10.5 or later, we can use /usr/libexec/java_home to return the location of the default JDK.

Where is Java Path Mac terminal?

In macOS, the JDK installation path is /Library/Java/JavaVirtualMachines/jdk-10. jdk/Contents/Home . The root directory of the JDK software installation.


2 Answers

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

export PATH=$JAVA_HOME/bin:$PATH

Add above 2 lines in ~/.bashrc or ~/.zshrc and reload the file using source command.

like image 20
Girija Sankar Panda Avatar answered Sep 28 '22 01:09

Girija Sankar Panda


I stumbled upon your question when trying to solve the same issue while migrating from bash to oh-my-zsh. The reason it's not there is that there is no code setting it for zsh but there was for bash. Generally theres something exporting JAVA_HOME whenever a new bash window is opened so it's always set for you. There is a good thread where this might be happening on the Unix & Linux StackExchange site.

To do the same thing in zsh, you can edit the .zshrc which is run every time zsh starts. I found a sample .zshrc which got me most of the way. The key line being:

export JAVA_HOME=`/usr/libexec/java_home`

Here is the file which I appended to the end of my existing ~/.zshrc file:

#zshrc, interactive shell settings
export ZSH=$HOME/.zsh

# emacs integration
[[ $EMACS = t ]] && unsetopt zle

# env
if [[ -e /usr/libexec/java_home ]]; then
  export JAVA_HOME=`/usr/libexec/java_home`
fi

if [[ -e /usr/local/lib/node_modules ]]; then
  export NODE_PATH=/usr/local/lib/node_modules
fi

# path
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
export PATH=/opt/usr/sbin:/opt/sbin:/opt/usr/bin:/opt/bin:$PATH
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
export PATH=$HOME/.cabal/bin:$PATH
export PATH=$HOME/.gem/ruby/1.8/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$HOME/.bin:$PATH

setopt null_glob
# source all files in zsh root
for include in $ZSH/*.zsh; do
  source $include
done

# source all non-controlled files
for include in $ZSH/private/*.zsh; do
  source $include
done
unsetopt null_glob

Then source ~/.zshrc to run in the current shell (or just start a new one) and you should be able to see that it is set with export | grep JAVA_HOME.

I also ended up running mkdir ~/.zsh to create the directory this is looking for and removing the .cabal and .gem lines as they were not needed for me.

like image 123
pherris Avatar answered Sep 28 '22 03:09

pherris