If you code in Java, you might be coding against Java 11, or 8, or maybe an even older version. You may also be prototyping code against newer versions of Java, the current version is 13. Java 14 will be released this month, and 15 later this year. At the same time, you might be investigating different builds of OpenJDK - there are several free alternatives. I use AdoptOpenJDK’s builds of OpenJDK which is an increasingly popular choice according to the JVM Ecosystem Report 2020.
You could also be trying out different build tools like Maven and Gradle. I use both, depending on the project.
坦率地说,管理所有这些可能很困难。 手动管理并非不可能,但是很奇怪,如果您弄错了,错误消息可能很难理解。
Enter SDKMAN! It’s a tool for managing the installation and selection of Software Development Kits - not just different versions and builds of Java itself, but tools for building, debugging, monitoring, documenting and deploying too. It is available for Windows, Linux and MacOS.
Installing SDKMAN!
Follow the installation instructions for your platform. If you use the terminal a lot, I highly recommend checking out Oh My Zsh which can add a ton of useful information and behaviour. There is an SDKMAN! plugin for Oh My Zsh which adds tab completion to the sdk
command which is really helpful. Enable this by finding the plugins=
line in the .zshrc
file in your home directory and adding sdk
. Mine reads:
plugins=(git sdk)
如果您确实要编辑该文件,则需要使用以下命令重新加载Zsh配置:源〜/ 。zshrc。
Installing a specific version of Java
假设我们要从AdoptOpenJDK安装最新版本的Java 11。 首先,使用以下命令检查其是否可用sdk列表java。 如果您设置了Oh My Zsh,则可以使用制表符完成此操作:
因此,来自AdoptOpenJDK的最新Java 11 OpenJDK构建具有标识符11.0.6.hs-adpt。 通过以下方式安装:
sdk install java 11.0.6.hs-adpt
SDKMAN! 将下载该版本的Java,并将其解压缩到计算机上的目录中。 您无需担心确切的位置,因为SDKMAN! 还将更新环境变量,以便您可以使用爪哇在终端立即:
➜ java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed mode)
Switching between Java versions
我还安装了最新的Java 14抢先体验版(定于2020年3月17日发布),以便尝试一些新功能。 我这样做的原因是:
sdk install java 14.ea.36-open
当我询问是否要将其用作默认版本时,我说“不”。 稍后,使用sdk use java <version>:
我想您会同意,这比手动管理下载,安装和环境变量要容易得多。
您可以立即查看正在使用的版本SDK电流:
➜ sdk current Using:java: 14.ea.36-open
Installing other tools
For different projects I use either Maven or Gradle as my build tool, and for debugging and profiling I sometimes use VisualVM. Install the latest versions of these with:
sdk install maven 3.6.3
sdk install gradle 6.2.2
sdk install visualvm 2.0
而且它们立即可用。
Using SDKMAN! with an IDE
要为IntelliJ IDEA或Eclipse中的项目选择特定版本的Java,您将需要确切知道SDKMAN! 已将您的Java安装解压缩。 这是$ HOME / .sdkman / candidates / java,其子目录以版本命名。 当前选择的Java版本也将以当前在该目录中:
➜ ls -l ~/.sdkman/candidates/java
total 8
drwxr-xr-x 10 mjg mjg 4096 Jan 15 12:14 11.0.6.hs-adpt
drwxrwxr-x 8 sdk mjg 4096 Mar 4 12:43 14.ea.36-open
lrwxrwxrwx 1 mjg mjg 52 Mar 4 12:35 current -> /home/mjg/.sdkman/candidates/java/14.ea.36-open
我建议通过将IDE中的项目配置为使用特定安装而不是“当前”安装来使事情更清晰。
对于IntelliJ IDEA,可以从“项目结构”对话框中完成。 从下拉列表中选择一个现有版本,或者通过从“ Project SDK”部分中选择“ New…”来添加新的Java版本:
对于Eclipse,这是通过转到“项目属性”,然后是“ Java构建路径”,“库”并选择“模块路径”来完成的:
您应该选择已经存在的版本(在此示例中为11..0.6.hs-adpt)并将其删除,然后再次选择“模块路径”,“添加库”并从对话框中选择“ JRE系统库”:
在“添加库”对话框中,选择“替代JRE”,然后单击“已安装JRE”以打开另一个对话框,您可以从中选择现有版本,也可以通过浏览至安装位置来添加新的Java安装:
Happy Hacking!
Using SDKMAN! can save you a lot of time and trouble if you work with multiple versions of Java and related tools. Maybe use that time to check out how to build cool things with Java and Twilio like a WhatsApp bot which does image recognition or sending daily SMS reminders?
I’d love to hear what you’re building, tell me about it on Twitter @MaximumGilliard or by email on mgilliard@twilio.com, or post it on Reddit to /r/twilio for the whole world to see.