如何安装和管理Java的多个版本(提示:jabba和jEnv)

我未来几个月的目标之一是提高我对Java的熟练程度。 就是说,这些年来,我通过反复试验将真正的知识带入了我目前的拼凑理解之中。

Before diving into Java training courses on Udemy, Youtube, etc, I wanted to make sure that I had a way to use different versions of the JDK on my machine. This was due, at least in part, to the changes Oracle made to their Java licensing and release cycle. There are now a variety of Java versions and vendor distributions available, and I wanted to find a way to easily switch between different versions of the JDK without needing to actually update my system’s version of Java or manually set JAVA_HOME each time.

Basically, the same way I can use different versions of Ruby, Node, and CFML, thanks to rbenv, nvm, and Command乙ox, respectively, it seemed I should be able to easily use different versions of Java, and not be tied to my system’s default version. So, like a good developer I did some Googling and stumbled across this helpful StackOverflow post, and ultimately settled on using both jabba and jEnv - the former to install versions and the latter to configure them on a per-directory basis. Here’s how it works:

jabba to install different JDK versions

Ĵabba is a cross-platform Java Version Manager. I found that it really excels at streamlining the process of installing different versions of Java. You can find the installation instructions along with some very helpful documenation on the Ĵabba Github repo.

安装完成后,您可以通过以下方式列出JDK的可用版本贾巴ls-远​​程:

$ jabba ls-remote
1.12.0
1.12.0-1
1.6.65
[email protected]
...
[email protected]

目前,有100多个结果,从字面上看是从A到Z的(即,从OpenJDK到Zulu OpenJDK)。

要安装特定版本,例如Corretto,请运行:

$ jabba install [email protected]
#installed to ~/.jabba/jdk/[email protected]/

在Linux / Mac上,JDK版本已下载/安装到〜/ .jabba。 请记住这一点,因为使用jEnv时需要知道这些位置。

可以通过URL或文件位置手动安装JDK的版本,例如Oracle的11.0.3(现在需要下载帐户)。 下载后,以下是我安装Oracle的方法:

$ jabba install [email protected]=tgz+file:///Users/MYUSER/Downloads/jdk-11.0.3_osx-x64_bin.tar.gz

Using jabba with Intellij on Mac

When I tried to add the jabba-installed JDKs to Intellij as SDKs, I ran into a small issue - because they’re located in a hidden folder, they’re not readily selectable via the file browser. Unsurprisingly, I wasn’t the first person to have this issue, a Reddit post in r/java pointed me to two answers:

  • 在Intellij中,在浏览JDK时〜/ .jabba,您可以输入Command-Shift-句号(⌘+⇧+。),它将显示隐藏的文件/文件夹,使其可以选择。 显然,这不适用于所有Intellij版本,因此还有另一种选择。在常规Finder中,导航到该文件夹〜/ .jabba/jdk/JDK@VERSION/Contents/Home-然后,您应该可以将其拖动到Intellij Finder窗口中,以便使用/选择它。

jEnv for per-directory JDK assignment

整理安装JDK版本后,我的下一个目标是使管理和使用它们变得简单。 首先要考虑的是是否可以将JDK版本分配给项目/目录,而不必再次考虑它。 为了我,环保搞定了。

So, what is jEnv? To pull a quote from its website:

jEnv是一个命令行工具,可帮助您忘记如何设置JAVA_HOME环境变量

因此,尽管jEnv没有安装不同版本的Java,但它可以更轻松地管理您计算机上已经存在的版本(例如jabba安装的版本)。 请注意,不幸的是,对于Windows用户,它当前不是跨平台的。

安装jEnv之后,我需要从jabba(以及从我的机器)添加JDK。 这是通过使用jenv添加命令并提供JDK版本的路径:

# Add a jabba JDK
$ jenv add /Users/MYUSER/.jabba/jdk/[email protected]/Contents/Home/
oracle64-11.0.3 added
11.0.3 added
11.0 added

# Add the system JDK
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/
oracle64-1.8.0.181 added
1.8.0.181 added
1.8 added

# List JDK versions
$ jenv versions

现在-这是我最兴奋的-我可以为目录设置Java版本,然后就无需再考虑了。 运作方式如下:

$ java -version
java version "1.8.0_181"

$ jenv local oracle64-11.0.3

$ java -version
java version "11.0.3" 2019-04-16 LTS

这可以通过添加一个.java版本文件到包含指定版本的目录。 jEnv会自动确保在文件夹(和子文件夹)中使用此版本。 设置并忘记它。

Wrapping it up

最终,我对该解决方案感到非常满意-它使我能够为项目进行一些初始的Java配置,然后忘记它并返回代码。 也就是说,我不是Java专家。 如果您有更好的方法或发现此方法有问题,请告诉我。

from: https://dev.to//mjclemente/how-to-install-and-manage-multiple-versions-of-java-hint-jabba-and-jenv-2ho0

你可能感兴趣的:(如何安装和管理Java的多个版本(提示:jabba和jEnv))