Install multi-version JDK on MAC and switch between them

Source: Internet
Author: User

Install multi-version JDK on MAC and switch between them

Environment:

Mac air, OS X 10.10, 64-bit

History:

In the past, Java on Mac was provided by Apple and only supported to Java 6, and the OS X 10.7 does not come with it (but is optional) (the original version is 1.6 ).

Later, Apple joined OpenJDK to continue to support Java 6, and Oracle was responsible for providing Java 7.

Enter java-version in the terminal to view the current JDK version information. If no JDK is installed, a prompt is displayed for installation. After clicking this button, the download page of oralce java is displayed.

Installation Package (note that the dmg installation package under mac is downloaded ):

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

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

Installation:

Dmg installation package. The installation process is as follows:

The final installation directory (the 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 method:

Vi. bash_profile enter the following content
Then execute source. bash_profile to apply the new configuration.

# JDK 6 for Mac by default (JDK 6 for Mac by default)
Export JAVA_6_HOME = '/usr/libexec/java_home-v 1.6'
# Set JDK 7
Export JAVA_7_HOME = '/usr/libexec/java_home-v 1.7'
# Set JDK 8
Export JAVA_8_HOME = '/usr/libexec/java_home-v 1.8'

# JDK 6 by default
Export JAVA_HOME = $ JAVA_6_HOME

# Dynamically switch the JDK version using the alias command
Alias jdk6 = "export JAVA_HOME = $ JAVA_6_HOME"
Alias jdk7 = "export JAVA_HOME = $ JAVA_7_HOME"
Alias jdk8 = "export JAVA_HOME = $ JAVA_8_HOME"

The default jdk is 1.6. After you run jdk 6, jdk 7, or jdk 8, you can see that the version has been switched to the corresponding version through java-version.

Recommended Method B:

1. Install jenv

Run: curl-s get. jenv. io | bash

Jenv reference (key is to facilitate other java tool management): https://github.com/linux-china/jenv/wiki/Chinese-Introduction

2. Go to the jenv directory and create the relevant directory:

Cd ~ /. Jenv/candidates/
Mkdir java
Cd java
Mkdir1.6
Mkdir1.7
Mkdir1.8

3. Run the following command:

Ln-s/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin ~ /. Jenv/candidates/java/1.6
Ln-s/Library/Java/JavaVirtualMachines/jdk1.7.0 _ 45.jdk/ Contents/Home/bin ~ /. Jenv/candidates/java/1.7
Ln-s/Library/Java/JavaVirtualMachines/jdk1.8.0 _ 25.jdk/ Contents/Home/bin ~ /. Jenv/candidates/java/1.8

Success:

1. The default jdk is the jdk you installed last.

2. Switch version: jenv use java 1.8

3. Set the missing version: jenv default java 1.6

Not recommended:

# The difference is: This part is written to the dead path
Export JAVA_6_HOME =/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Export JAVA_7_HOME =/Library/Java/JavaVirtualMachines/jdk1.7.0 _ 45.jdk/ Contents/Home
Export JAVA_8_HOME =/Library/Java/JavaVirtualMachines/jdk1.8.0 _ 25.jdk/ Contents/Home

# JDK 6 by default
Export JAVA_HOME = $ JAVA_6_HOME

# Dynamically switch the JDK version using the alias command
Alias jdk6 = "export JAVA_HOME = $ JAVA_6_HOME"
Alias jdk7 = "export JAVA_HOME = $ JAVA_7_HOME"
Alias jdk8 = "export JAVA_HOME = $ JAVA_8_HOME"

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

Example:

// View the default jdk installation path
/Usr/libexec/java_home

// View the jdk 1.6 installation path
/Usr/libexec/java_home-v 1.6

In earlier versions, the fixed/Library/Java/Home directory should be used because this command is not available, that is, it should be directly written to the environment variable.

// The following directories are actually equivalent links, all pointing to the default JDK. However, hard encoding is not recommended.
JAVA_HOME =/Library/Java/Home
JAVA_HOME =/System/Library/Frameworks/JavaVM. framework/Versions/CurrentJDK/Home
JAVA_HOME =/System/Library/Frameworks/JavaVM. framework/Home

Because different Java versions and different Java implementations may be installed in different directories, hard-coded directories have the following Disadvantages:

After installing or upgrading the new Java, You need to reset JAVA_HOME (especially the directory with version numbers)
Cannot adapt to different Java implementations (Apple and Oracle)
Other analyses:
1. Open the terminal and run the following command: which java,/usr/bin/java
2. Run cd/usr/bin/And then ls-ll | grep java. You can see that the java command is link to/System/Library/Frameworks/JavaVM. framework/Versions/Current/Commands/java
3. Run cd/System/Library/Frameworks/JavaVM. framework/Versions/, and then execute ls-ll. You can see Current-> A, that is, link the Current directory to directory. Directory A exists when the first JDK is installed. It is used for managing multiple JDK versions (it is assumed that JAVA_HOME is used ).
In addition:
There are multiple directories of different (low) Versions under/System/Library/Frameworks/JavaVM. framework/Versions/, but they are linked to the CurrentJDK directory. In short, these Java are provided by Apple, and the Java provided by Oracle or OpenJDK is not in this directory.
It is said that you can set the current jdk in the "Java Preferences" dialog box in the system settings, and then automatically adjust the content of directory A, but I have not seen it.
Java 7 requires Mac OS X 10.7.3 (Lion) and later versions to be installed. Running Java in a browser requires a 64-bit browser, So Chrome, Safari, or Firefox is not supported.

This article permanently updates the link address:

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.