How to set or change the default Java (JDK) versio

2019-01-01 11:16发布

How can you change the default version of Java on a mac?

标签: java macos
20条回答
栀子花@的思念
2楼-- · 2019-01-01 11:56

Use jenv is an easy way.

1.Install jenv

    curl -s get.jenv.io | bash

2.Config jenv

    cd ~/.jenv/candidates/
    mkdir java
    cd java
    mkdir 1.7
    mkdir 1.8

3.Symlink the jdk path

    ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin ~/.jenv/candidates/java/1.7
    ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin ~/.jenv/candidates/java/1.8

4.You are all set

switch command:

jenv use java 1.8

set default:

jenv default java 1.7

查看更多
素衣白纱
3楼-- · 2019-01-01 11:58

From the Apple's official java_home(1) man page:

**USAGE**

   /usr/libexec/java_home  helps  users  set a $JAVA_HOME in their login rc files, or provides a way for
   command-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-
   tecture  requirement.  The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,
   which is useful for starting Java command-line tools from launchd plists without hardcoding the  full
   path to the Java command-line tool.

   Usage for bash-style shells:
          $ export JAVA_HOME=`/usr/libexec/java_home`

   Usage for csh-style shells:
          % setenv JAVA_HOME `/usr/libexec/java_home`
查看更多
千与千寻千般痛.
4楼-- · 2019-01-01 11:59

Adding to the above answers, I put the following lines in my .bash_profile which makes it really convenient to switch (including @elektromin's comment for java 9):

alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"

After inserting, execute $ source .bash_profile

I can switch to Java 8 by typing the following:

$ j8
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
查看更多
皆成旧梦
5楼-- · 2019-01-01 11:59

Based on @markhellewell answer I created a couple of alias functions that will do it for you. Just add these to your shell startup file

#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() { 
  echo "Switching java version"; 
  export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`; 
  java -version; 
}

https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4

查看更多
与风俱净
6楼-- · 2019-01-01 12:01
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
}

put this in your ~/.profile and use it in your terminal like so setjdk 1.8, setjdk 1.7, setjdk 9 etc etc...

If you don't have removeFromPath then it is:

function removeFromPath() { export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;") }

查看更多
伤终究还是伤i
7楼-- · 2019-01-01 12:02

If you have multiple versions and you want to run something by using a specific version, use this example:

/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar
查看更多
登录 后发表回答