Java version management tool jEnv in OS X

 

 

We often need to use multiple versions of JDK in Mac, 6 is downloaded from Apple, 7 and 8 are downloaded from Oracle.

The jdk installation directory installed by yourself: /Library/Java/JavaVirtualMachines 



 

If you are not upgrading through the Java Control Panel in System Preferences, then you will need to manually configure JAVA_HOME to specify a different Java version. Otherwise, when you install the new version of JDK, you will find that the java -version command still displays the previous version. Manually configuring JAVA_HOME is quite troublesome.

 

jenv is a tool dedicated to configuring the JAVA_HOME environment variable. You can use it to manage Java versions on Mac.

1. Installation

$ brew install jenv

 

2. Configuration

Using Bash case:

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

 

This will append an export command and eval command to the ~/.bash_profile file. The former appends the path of jenv to the environment variable PATH, and the latter executes the jenv init-command. In this way, the jenv command can be called every time a bash terminal window is opened, and the jenv init - command is executed once by default.

 

Using Zsh case:

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc

 

 

3. Check the java version

$ jenv versions
* system (set by /Users/cactus/.jenv/version)

 

You can see that jenv only lists the built-in Java version (system), because the other two versions are installed, but we need to manually add them to jenv so that jenv can manage them. * Indicates the currently selected version.

 

4. Add java version to jenv

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

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

 

 

5. Specify the java version (per directory)

$ jenv local 1.8.0.121

 

 

Then check that the next version is successfully switched:

$ jenv versions
  system
  1.7
  1.7.0.80
  1.8
* 1.8.0.121 (set by /Users/cactus/.java-version)
  oracle64-1.7.0.80
  oracle64-1.8.0.121

 

$ java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
yangjundeMacBook-Pro:~ yangjunzhu$

 

6. Display the full path of the current version of Java

$ jenv which java
/Users/cactus/.jenv/versions/1.8.0.121/bin/java

 

This is of course not a real path, but a hard link. You can find the /Users/cactus/.jenv/versions/ directory and find that all java versions are listed here, and these hard links (equivalent to windows shortcuts) point to the corresponding java installation directory.

 

7. Delete the java version

$ jenv remove oracle64-1.8.0.121

 

8. Specify the global version

$jenv global 1.8.0.121

 Thus, the default java version is 1.8.0.121.

 

9、

If you want to use version 1.7 in a project, you can create a new .java-version file in the project folder and edit the contents of the file to save. This way, jenv will automatically use 1.7 as the current version (ie the local version) when you enter this folder.

 

 

Official website:

http://www.jenv.be/

 Source code:

https://github.com/gcuisinier/jenv

ps: If you encounter an error, you can sudo

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326686397&siteId=291194637