Install multiple versions of JDK and switch several ways under Mac

Source: Internet
Author: User

Environment:

MAC Pro,os X 10.11.6,64 bit

History:

Java in the past was provided by Apple itself, only to Java 6, and OS X 10.7 started with a system that did not come with (but is optional) (the original comes with 1.6).

Later, Apple joined OpenJDK to continue supporting Java 6, while Java 7 would be provided by Oracle.

Enter Java-version in the terminal to view the current JDK version information, if you have not installed any JDK, you will be prompted to install, click will jump to oralce Java download page.

Install the package (note that the DMG installation package is downloaded under Mac):

JDK 1.6 (Verify OS X 10.10 available): http://support.apple.com/kb/DL1572

JDK 1.7&1.8 (1.7 below page): http://www.oracle.com/technetwork/java/javase/downloads/index.html

Installation:

DMG installation package, installation process such as general procedures, slightly

Final installation directory (version number varies slightly depending on the situation):

JDK 1.6:

/system/library/java/javavirtualmachines/1.6.0.jdk

JDK 1.7&1.8:

/library/java/javavirtualmachines/jdk1.7.0_45.jdk

/library/java/javavirtualmachines/jdk1.8.0_25.jdk

Recommended way A:

Java code
    1. VI. Bash_profile Enter the following:
    2. Then execute the source. Bash_profile Effective New configuration

Java code
  1. # mac Default JDK 6 (Mac comes with a jdk6 version by default)
  2. Export Java_6_home= '/usr/libexec/java_home-v 1.6 '
  3. # set JDK 7
  4. Export java_7_home= '/usr/libexec/java_home-v 1.7 '
  5. # set JDK 8
  6. Export java_8_home= '/usr/libexec/java_home-v 1.8 '
  7. #默认JDK 6
  8. Export Java_home= $JAVA _6_home
  9. #alias命令动态切换JDK版本
  10. Alias jdk6="Export java_home= $JAVA _6_home"
  11. Alias jdk7="Export java_home= $JAVA _7_home"
  12. Alias jdk8="Export java_home= $JAVA _8_home"

The default JDK is 1.6, and after you perform jdk6 or Jdk7 or JDK8, you can see that you have switched to the appropriate version by Java-version.

Recommended Way B:

1, installation Jenv

Execution: Curl-s Get.jenv.io | Bash

Jenv Reference (key is convenient for other Java tools Management): Https://github.com/linux-china/jenv/wiki/Chinese-Introduction

2, enter the Jenv directory, and then build the relevant directory:

Java code
    1. CD ~/.jenv/candidates/
    2. mkdir Java
    3. CD Java
    4. mkdir 1.6
    5. mkdir 1.7
    6. mkdir 1.8

3. Execute the following command:

Java code
    1. Ln-s/system/library/java/javavirtualmachines/1.6. 0.jdk/contents/home/bin ~/.jenv/candidates/java/1.6
    2. Ln-s/library/java/javavirtualmachines/jdk1. 7.0_45.jdk/contents/home/bin ~/.jenv/candidates/java/1.7
    3. Ln-s/library/java/javavirtualmachines/jdk1. 8.0_25.jdk/contents/home/bin ~/.jenv/candidates/java/1.8

Done:

1, the first default JDK is usually the last JDK you installed.

2. Switch version: Jenv use Java 1.8

3. Setup missing version: jenv default Java 1.6

Not recommended way:

Java code
  1. #区别在于: This section is a write-dead path
  2. Export java_6_home=/system/library/java/javavirtualmachines/1.6. 0.jdk/contents/home
  3. Export JAVA_7_HOME=/LIBRARY/JAVA/JAVAVIRTUALMACHINES/JDK1. 7.0_45.jdk/contents/home
  4. Export JAVA_8_HOME=/LIBRARY/JAVA/JAVAVIRTUALMACHINES/JDK1. 8.0_25.jdk/contents/home
  5. #默认JDK 6
  6. Export Java_home= $JAVA _6_home
  7. #alias命令动态切换JDK版本
  8. Alias jdk6="Export java_home= $JAVA _6_home"
  9. Alias jdk7="Export java_home= $JAVA _7_home"
  10. Alias jdk8="Export java_home= $JAVA _8_home"

According to Apple's official instructions, Mac OS X 10.5 and later versions should use the/usr/libexec/java_home command to determine Java_home (the recommended way A is based on this principle).

Cases:

Java code
    1. To view the installation path for the default JDK
    2. /usr/libexec/java_home
    3. To view the installation path for JDK 1.6
    4. /USR/LIBEXEC/JAVA_HOME-V 1.6

In the previous version, because there is no such command, you should use a fixed/library/java/home directory, that is, directly in the environment variable write dead.

Java code
    1. The following directories are actually equivalent link relationships, all pointing to the default JDK. But hard-coded way, not recommended.
    2. Java_home=/library/java/home
    3. Java_home=/system/library/frameworks/javavm.framework/versions/currentjdk/home
    4. Java_home=/system/library/frameworks/javavm.framework/home

Because different Java versions and different Java implementations may be installed in different directories, using a hard-coded directory can have the following drawbacks:

    • Java_home (especially directories with version numbers) need to be reset after installing or upgrading new Java
    • Unable to adapt to different Java implementations (Apple and Oracle)
Other Analysis:1, open the terminal input execution: which Java, display/usr/bin/java2, execute cd/usr/bin/, and then execute Ls-ll |grep java, can see java This command is link to/system/library/ FRAMEWORKS/JAVAVM.FRAMEWORK/VERSIONS/CURRENT/COMMANDS/JAVA3, execution: cd/system/library/frameworks/javavm.framework/ versions/Enter, and then execute the LS-LL, you can see current, a, that is, the current directory link to a directory. The A directory exists when the first JDK is installed and is used for multi-version management of JDK (guessing is used to java_home). Additionally: there are several different (low) versions of the directories under/system/library/frameworks/javavm.framework/versions/, but they are all linked to the CURRENTJDK directory. In short, these Java are provided by Apple Java,oracle or OpenJDK provided Java is not in this directory. It is said that in the system setup the Java Preferences dialog can be used to set the current JDK and then automatically adjust the contents of a directory, but I have not seen it. Java 7 requires Mac OS X 10.7.3 (Lion) and later to install. Running Java in the browser requires a 64-bit browser, so chrome is not supported, Safari or Firefox is no problem.

Install multiple versions of JDK and switch several ways under Mac

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.