Win环境Java版本切换

jenv的使用

Posted by Duu on March 7, 2024

Java多版本切换工具

Java多版本切换时,需要每次手动的修改系统环境,这很麻烦, Jenv是一款Java多版本切换工具

下载Releases的最新版本

Jenv - Github

Jenv使用方法

相关命令:

jenv -help
JEnv is changing your environment variables. This process could take longer but it happens only when a jenv executable is found in your path
"jenv list"                            List all registered Java-Envs.
"jenv add <name> <path>"               Adds a new Java-Version to JEnv which can be refferenced by the given name
"jenv remove <name>"                   Removes the specified Java-Version from JEnv
"jenv change <name>"                   Applys the given Java-Version globaly for all restarted shells and this one
"jenv use <name>"                      Applys the given Java-Version locally for the current shell
"jenv local <name>"                    Will use the given Java-Version whenever in this folder. Will set the Java-version for all subfolders as well
"jenv link <executable>"               Creates shortcuts for executables inside JAVA_HOME. For example "javac"
"jenv uninstall <name>"                Deletes JEnv and restores the specified java version to the system. You may keep your config file
"jenv autoscan [--yes|-y] ?<path>?"    Will scan the given path for java installations and ask to add them to JEnv. Path is optional and "--yes|-y" accepts defaults.

踩坑

  1. 使用jenv更改 JDK 版本后,java版本会更改,但是不会改变javac版本,会出现Java 版本为11,而javac版本仍为1.8,当手动javac.java文件编译为.class文件后,使用java命令运行.class文件会报错。

这时手动修改JAVA_HOME环境变量是没有用的,需要将PATH环境变量中javac文件路径提前到第一个。