在 OSX 上将 shell 从 Bash 更改为 Zsh 时找不到 Java_HOME?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30537995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:05:10  来源:igfitidea点击:

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

macosbashzsh

提问by piyushdubey

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?

这很奇怪,我已经为我的 mac 设置了 JAVA_HOME,当我使用 bash shell 时可以找到它,但是如果我更改 shell,我会收到一条消息,说 JAVA_HOME 未设置。这里会发生什么?

采纳答案by K139

When you set JAVA_HOME in a shell, then it is active and available only for that context, and it will be gone when you close that shell.

当您在 shell 中设置 JAVA_HOME 时,它就处于活动状态并且仅可用于该上下文,并且在您关闭该 shell 时它将消失。

Instead either change global environment (or) your .bashrc to include it. So that every time you start a shell, the variable will be available.

相反,要么更改全局环境(或)您的 .bashrc 以包含它。这样每次启动 shell 时,该变量都将可用。

edit the .profileor .bash_profileto include the JAVA_HOME.

编辑.profile.bash_profile以包含 JAVA_HOME。

export JAVA_HOME=`/usr/lib....`

and also below command will return the path for java home directory.

并且下面的命令将返回java主目录的路径。

/usr/libexec/java_home -v 1.7

where 1.7 is the version you want.

其中 1.7 是您想要的版本。

回答by pherris

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 zshbut there was for bash. Generally theres something exporting JAVA_HOMEwhenever a new bashwindow 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.

在从 bash 迁移到 oh-my-zsh 的过程中尝试解决相同的问题时,我偶然发现了您的问题。它不存在的原因是没有代码设置它,zsh但是有 for bash。通常,JAVA_HOME每当bash打开新窗口时都会导出一些内容,因此它始终为您设置。在Unix & Linux StackExchange 站点上有一个很好的线程可能会发生这种情况。

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

要在 中执行相同的操作zsh,您可以编辑.zshrc每次zsh启动时运行的。我找到了一个让我大获全胜的样本.zshrc。关键线是:

export JAVA_HOME=`/usr/libexec/java_home`

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

这是我附加到现有~/.zshrc文件末尾的文件:

#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 ~/.zshrcto 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.

然后source ~/.zshrc在当前的 shell 中运行(或者只是启动一个新的),你应该能够看到它设置了export | grep JAVA_HOME.

I also ended up running mkdir ~/.zshto create the directory this is looking for and removing the .cabaland .gemlines as they were not needed for me.

我还最终运行mkdir ~/.zsh以创建正在查找的目录并删除.cabal.gem行,因为它们对我来说是不需要的。

回答by dmytro Minz

I have just installed Mac OS Catalina Version 10.15 and found that environment variables such as JAVA_HOME and others that have been set in my .bash_profile :

我刚刚安装了 Mac OS Catalina 10.15 版,发现在我的 .bash_profile 中设置了诸如 JAVA_HOME 等环境变量:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
export ANDROID_HOME=/Users/mynziak/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export M2_HOME=/usr/local/Cellar/maven/3.6.2/libexec
export M2=${M2_HOME}/bin
export PATH=${PATH}:${M2_HOME}/bin

are not set in fact!

实际上没有设置!

I saw %in terminal instead of general $that means you are using a zsh shell instead of bash shell. With Catalina zsh is now the default shell and bash will be completely gone in the future.

%在终端而不是一般$中看到这意味着您使用的是 zsh shell 而不是 bash shell。Catalina zsh 现在是默认的 shell,而 bash 将在未来完全消失。

oh-my-zsh shell: https://ohmyz.sh/

oh-my-zsh 外壳:https: //ohmyz.sh/

So you have to setup all environment variables in .zshrcfile.

所以你必须在.zshrc文件中设置所有环境变量。

I just copy-pasted every variables from .bash_profilein to .zshrcand re-opened terminal.

我只是将.bash_profile中的每个变量复制粘贴到.zshrc并重新打开终端。

Files .bash_profileand .zshrcare hidden (cmd+shift+. - show hidden files in finder) but can be found in path:

文件 .bash_profile.zshrc是隐藏的(cmd+shift+. - 在 finder 中显示隐藏文件)但可以在路径中找到:

/Users/mynziak/.zshrc

/用户/mynziak/.zshrc

but use own username!

但使用自己的用户名!

enter image description here

在此处输入图片说明