关于Linux系统下jdk版本切换问题(alternatives命令的使用)

时间:2023-03-08 16:41:24
关于Linux系统下jdk版本切换问题(alternatives命令的使用)
[root@danny lib]# cd /usr/lib/jvm
[root@danny jvm]#
[root@danny jvm]# ls
java java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64 jre-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64
java-1.7. java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug jre-1.8.
java-1.7.-openjdk java-openjdk jre-1.8.-openjdk
java-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64 jre jre-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64
java-1.8. jre-1.7. jre-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug
java-1.8.-openjdk jre-1.7.-openjdk jre-openjdk
[root@danny jvm]# ll
total
lrwxrwxrwx root root Nov : java -> /etc/alternatives/java_sdk
lrwxrwxrwx root root Nov : java-1.7. -> /etc/alternatives/java_sdk_1.7.0
lrwxrwxrwx root root Nov : java-1.7.-openjdk -> /etc/alternatives/java_sdk_1..0_openjdk
drwxr-xr-x root root Nov : java-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64
lrwxrwxrwx root root Sep : java-1.8. -> /etc/alternatives/java_sdk_1.8.0
lrwxrwxrwx root root Sep : java-1.8.-openjdk -> /etc/alternatives/java_sdk_1..0_openjdk
drwxr-xr-x root root Nov : java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64
drwxr-xr-x root root Sep : java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug
lrwxrwxrwx root root Nov : java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx root root Nov : jre -> /etc/alternatives/jre
lrwxrwxrwx root root Nov : jre-1.7. -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx root root Nov : jre-1.7.-openjdk -> /etc/alternatives/jre_1..0_openjdk
lrwxrwxrwx root root Nov : jre-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64 -> java-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre
lrwxrwxrwx root root Sep : jre-1.8. -> /etc/alternatives/jre_1.8.0
lrwxrwxrwx root root Sep : jre-1.8.-openjdk -> /etc/alternatives/jre_1..0_openjdk
lrwxrwxrwx root root Sep : jre-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64 -> java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64/jre
lrwxrwxrwx root root Sep : jre-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug -> java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug/jre
lrwxrwxrwx root root Nov : jre-openjdk -> /etc/alternatives/jre_openjdk
[root@danny jvm]# ls /etc/alternatives/java_sdk
bin demo include jre lib sample src.zip tapset
[root@danny jvm]# alternatives --config java (版本切换) There are programs which provide 'java'. Selection Command
-----------------------------------------------
java-1.8.-openjdk.x86_64 (/usr/lib/jvm/java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug/jre/bin/java)
*+ java-1.8.-openjdk.x86_64 (/usr/lib/jvm/java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64/jre/bin/java) (当前版本)
java-1.7.-openjdk.x86_64 (/usr/lib/jvm/java-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java) Enter to keep the current selection[+], or type selection number: 3 (切换为1.7版本)
[root@danny jvm]# alternatives --config java (以下可以看到+号在1.7版本处,不变还是输入3退出) There are programs which provide 'java'. Selection Command
-----------------------------------------------
java-1.8.-openjdk.x86_64 (/usr/lib/jvm/java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64-debug/jre/bin/java)
* java-1.8.-openjdk.x86_64 (/usr/lib/jvm/java-1.8.-openjdk-1.8.0.181-.b13.el7_5.x86_64/jre/bin/java)
+ java-1.7.-openjdk.x86_64 (/usr/lib/jvm/java-1.7.-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java) Enter to keep the current selection[+], or type selection number:
[root@danny jvm]# java -version (可以看到已经变为1.7版本了)
java version "1.7.0_191"
OpenJDK Runtime Environment (rhel-2.6.15.4.el7_5-x86_64 u191-b01)
OpenJDK -Bit Server VM (build 24.191-b01, mixed mode)

注释:在探究linux中jdk版本切换时发现了如下一行

lrwxrwxrwx   root root    Nov   : java -> /etc/alternatives/java_sdk

网上搜了以下alternatives这个文件是什么东东时,才发现这是一个可以进行jdk版本切换的命令,命令格式如下:

alternatives --config java