Java 7 和 Java 8 可以在 OSX 上共存吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20974607/
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-08-13 05:39:39  来源:igfitidea点击:

Can Java 7 and Java 8 co-exist on OSX

javamacosjava-8

提问by Dan

I've installed Java 8 for development purposes but now I'd like to use Java 7 again.

我已经为开发目的安装了 Java 8,但现在我想再次使用 Java 7。

How do I do this?

我该怎么做呢?

It seems to be insanely difficult.

这似乎是非常困难的。

Many thanks in advance. (I've tried using guigarage and that doesn't work)

提前谢谢了。(我试过使用 guigarage 并不起作用)

采纳答案by Samy Dindane

From a terminal: export JAVA_HOME=`/usr/libexec/java_home -v 1.x`, where x is the Java version.

从终端:export JAVA_HOME=`/usr/libexec/java_home -v 1.x`,其中 x 是 Java 版本。

I personally have a shell function that does that for me:

我个人有一个 shell 函数可以为我做到这一点:

use-java () {
    export JAVA_HOME=`/usr/libexec/java_home -v 1.`
}

I just have to call use-java 7or use-java 8in order to change my current shell's Java version.

我只需要调用use-java 7use-java 8来更改我当前的 shell 的 Java 版本。

回答by user987339

After installation, open the Java Preferences (Launchapad/Others):

安装后,打开 Java Preferences (Launchapad/Others):

enter image description here

在此处输入图片说明

and drag the preferred version on top of list:

并将首选版本拖到列表顶部:

enter image description here

在此处输入图片说明

回答by Jason Sperske

Here is an excellent answer for how to switch Java version from the command line in OSX Mavericks (sourceby Neeme Praks):

下面是如何从在OSX小牛(命令行切换Java版本优异的答案通过Neeme Praks):

Edit your ~/.bash_profileand add the following:

编辑您的~/.bash_profile并添加以下内容:

function setjdk() {  
  if [ $# -ne 0 ]; then  
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'  
   if [ -n "${JAVA_HOME+x}" ]; then  
    removeFromPath $JAVA_HOME  
   fi  
   export JAVA_HOME=`/usr/libexec/java_home -v $@`  
   export PATH=$JAVA_HOME/bin:$PATH  
  fi  
  echo JAVA_HOME set to $JAVA_HOME  
  java -version  
 }  
 function removeFromPath() {  
  export PATH=$(echo $PATH | sed -E -e "s;:;;" -e "s;:?;;")  
 }

(add above function to your .bash_profile)

(将上述功能添加到您的.bash_profile

Usage:

用法:

$ setjdk 1.7

回答by flup

Use jEnv.

使用jEnv

If your system runs homebrew, you can install it using

如果您的系统运行homebrew,则可以使用

brew install jenv

(You may need to run brew updateto get the latest recipes first)

(您可能需要先运行brew update以获取最新食谱)

Add it to your bash profile using

使用以下命令将其添加到您的 bash 配置文件中

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Start a new shell to make this change to the profile effective.

启动一个新的 shell 以使对配置文件的此更改生效。

You can then add jdk's like this:

然后你可以像这样添加jdk:

jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

list the available versions using

使用列出可用版本

jenv versions

And switch between environments using

并使用在环境之间切换

jenv global oracle64-1.8.0.25

There's plenty more custom options, like switching per directory or temporarily in a single shell, see http://www.jenv.befor those.

还有更多自定义选项,例如按目录切换或临时在单个 shell 中切换,请参阅http://www.jenv.be

jEnv works by creating shim scripts for the java executables and putting them in the front of the path. Some 3rd party java tools like ant and maven rely on JAVA_HOME. To make sure JAVA_HOME gets set properly, run

jEnv 的工作原理是为 java 可执行文件创建 shim 脚本并将它们放在路径的前面。一些像 ant 和 maven 这样的 3rd 方 Java 工具依赖于 JAVA_HOME。要确保 JAVA_HOME 设置正确,请运行

jenv enable-plugin export

There's also jenv plugins for tools like maven and groovy.

还有用于 maven 和 groovy 等工具的 jenv 插件。