如何在Mac OS 10.8.4上切换java版本

时间:2013-08-06 00:07:56

标签: java macos

我需要在Mac OS X 10.8.4上切换Java版本,但我不确定如何,我现在的版本是1.6.0_51,但我想切换到1.6.0_45

$java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)

/usr/libexec/java_home -v 1.6.0_43
Unable to find any JVMs matching version "1.6.0_43".
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

我认为我的Mac上没有1.6.0_43这样的版本?如何安装然后切换到它?谢谢!

4 个答案:

答案 0 :(得分:4)

正如本网站上的描述所说:“......通过shell切换这个Java版本真的很痛苦......”所以这个人做了一个工具来进行切换。由于我不知道你的推理(如果你真的必须通过命令行完成),它可能不会过于有用,但快速谷歌搜索显示this downloadable tool from their website.

答案 1 :(得分:4)

要查看已安装的版本,请使用:

/usr/libexec/java_home -V

例如,为我展示了这一点:

15:04 $ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    10.0.1, x86_64: "Java SE 10.0.1"    /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
    1.7.0_80, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

然后只需在您的~/.bashrc~/.bash_profile中输入

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

至于获得1.6版(或几乎所有以前的版本),我认为从这个日期(2018年11月)开始,您将不走运。我可以进入https://jdk.java.net/8/,但是任何较早的版本都是404(最新版本是11)。我确信您可以搜索更多内容,尽管据我所知,一旦他们(Oracle)停止使用某个版本,他们将不再维护该版本,因为这可能会带来安全风险。

一旦您拥有DL Mac Mac .dmg,我确定它安装在正确的位置,并且可以通过java_home看到。

https://stackoverflow.com/a/47699905/1019307上有信息讨论使用homebrew安装Java并提到Java6(帖子日期为2017年12月)。

答案 2 :(得分:1)

  1. 安装您需要的其他Java版本。

  2. 我不得不在1.6_XX和1.7_XX之间切换,所以为此写了一个小脚本。它的工作做得很好。你必须更换一些东西,但也许它可以帮助你:

    #!/bin/bash
    #v1.0.3
    #by Sapphire
    
    # /!\ Configuration /!\
    # vim ~/.bash_profile :
    # alias changej="sudo ~/Documents/changeJavaVers.sh"
    # sudo visudo -f /etc/sudoers :
    # #Custom privilege
    # user ALL=NOPASSWD: /Users/username/Documents/changeJavaVers.sh
    # change java dir's at (1) and (2)
    
    echo -e "\n***********************************************************"
    echo "*                EASY JAVA VERSION CHANGER                *"
    echo "***********************************************************"
    
    echo "Enter java version (6, 7, ..) or 'set' for settings"
    echo -n "Version: "
    read vers
    if [ "$vers" == "set" ]
    then
        echo "settings not implemented yet"
        #TODO open settings
        #set java home path, add java versions
        #current home: echo $JAVA_HOME
    elif (($vers == 6))
    then
        #(1)
        ln -fhsv /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
        export JAVA_HOME=`/usr/libexec/java_home -v 1.6+ -d32`
        echo -e "\n-----------------------------------------------------------"
        echo "                       JAVA VERSION"
        echo "-----------------------------------------------------------"
        java -version
        echo -e "\n-----------------------------------------------------------"
        echo "                        JAVA HOME"
        echo "-----------------------------------------------------------"
        echo $JAVA_HOME
        echo "***********************************************************"
    elif (($vers == 7))
    then
        #(2)
        ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
        export JAVA_HOME=`/usr/libexec/java_home`
        echo -e "\n-----------------------------------------------------------"
        echo "                       JAVA VERSION"
        echo "-----------------------------------------------------------"
        java -version
        echo -e "\n-----------------------------------------------------------"
        echo "                        JAVA HOME"
        echo "-----------------------------------------------------------"
        echo $JAVA_HOME
        echo -e "\n***********************************************************"
    
    else
        echo -e "\ninvalid input - available java versions: 6, 7"
    fi
    
  3. 修改 在这里有更新的版本。适用于OSX 10.9.3

    #!/bin/bash
    #v1.1.0
    #last change: 12 June 2014
    #by Sapphire
    
    # /!\ Configuration /!\
    # vim ~/.bash_profile :
    # alias changej="sudo ~/Documents/changeJavaVers.sh"
    # sudo visudo -f /etc/sudoers :
    # #Custom privilege
    # user ALL=NOPASSWD: /Users/username/Documents/changeJavaVers.sh
    # change java directories (1), (2)
    
    echo -e "\n***********************************************************"
    echo "*                EASY JAVA VERSION CHANGER          v1.1.0 *"
    echo "***********************************************************"
    
    echo "Enter java version (6, 721, 745, 760) or 'set' for settings"
    echo -n "Version: "
    read vers
    if [ "$vers" == "set" ]
    then
        echo "settings not implemented yet"
        #TODO open settings
        #set java home path, add java versions
        #current home: echo $JAVA_HOME
    elif (($vers == 6))
    then
        #(1)
        ln -fhsv /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
    elif (($vers == 721))
    then
        #(1)
        ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
    elif (($vers == 745))
    then
        #(1)
        ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
    elif (($vers == 760))
    then
        #(1)
        ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
    else
        echo -e "\ninvalid input - available java versions: (6, 721, 745, 760)"
        exit
    fi
    #(2)
    export JAVA_HOME='/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home'
    echo -e "\n-----------------------------------------------------------"
    echo "                       JAVA VERSION"
    echo "-----------------------------------------------------------"
    java -version
    echo -e "\n-----------------------------------------------------------"
    echo "                        JAVA HOME"
    echo "-----------------------------------------------------------"
    echo $JAVA_HOME
    echo -e "\n***********************************************************"
    exit
    

答案 3 :(得分:0)

无需下载全新版本。你真正想要的是:

-target version
Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7).
The default for -target depends on the value of -source:

If -source is not specified, the value of -target is 1.7
If -source is 1.2, the value of -target is 1.4
If -source is 1.3, the value of -target is 1.4
If -source is 1.5, the value of -target is 1.7
If -source is 1.6, the value of -target is 1.7
For all other values of -source, the value of -target is the value of -source.  

来自this来源。 -target-source允许控制目标JVM和源代码级别。正如消息来源所说,target的值将默认为您提供的值,但您可以根据需要控制两者。

如果此选项无法为您提供足够的特定版本,但我无法想象为什么您需要这样的确切版本,那么您只需要咬紧牙关并下载正确的版本自己