超详细!Linux上安装Java JDK21 环境!

发布于:2025-02-26 ⋅ 阅读:(17) ⋅ 点赞:(0)

在linux上安装Java环境 -JDK21

从官网上下载 JDK21-Linux版本

我们首先从官网上下载 linux JDK 的安装版本,下载地址:
JDK官方下载地址
在这里插入图片描述

JDK21 上传到虚拟机 Centos上

将下载好的 JDK21-Linux 版本 上传到我们的linux服务器上,例如你本地的虚拟机中安装的linux 环境

上传工具你可以使用 xshellxftp; 你也可以使用 Mobaxterm

下载Mobaxterm链接地址:

Mobaxterm官网下载地址

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

填写好虚拟机linux的ip地址,用户名,点击确定,会让你填写密码,填写完毕后即可连接对应服务器

上传JDK21-Linux

在这里插入图片描述

将本地文件上传到对应的linux服务器上

在这里插入图片描述

创建JDK21解压目录并解压

首先切换到你下载的JDK目录下

在这里插入图片描述
创建jdk存放目录

mkdir /usr/local/java

将压缩包解压到上述目录当中

tar -zxvf jdk-21_linux-64_bin.tar.gz -C /usr/local/java

切换到 jdk 存放目录

cd /usr/local/java

查看结果如下,证明解压成功

[root@localhost java]# ls jdk-21.0.5/
bin  conf  include  jmods  legal  lib  LICENSE  man  README  release

配置JAVA环境变量

创建软连接

一般linux可能会有自己自带的java环境,是jdk1.8的,它已经占用了/usr/bin/java 的这个软链接

因此你需要先删除这个软连接

rm -rf /usr/bin/java

然后重新创建 java 软链接到 /usr/bin/ 目录下

ln -s /usr/local/java/jdk-21.0.5/ /usr/bin/java
编辑系统配置文件
vi /etc/profile

按下 键盘 i 进入可编辑模式;按下shift + G 快速定位到文件结尾处

然后输入以下内容

export JAVA_HOME=/usr/bin/java
export PATH=$JAVA_HOME/bin:$PATH

输入完毕之后,按下 esc,进入到 不可编辑状态; 按下shift + :(shift + 冒号);此时在键盘输入字母 wq(wq表示保存退出的意思)

激活配置文件
source /etc/profile

验证java安装是否成功

java --version

若输出以下内容

[root@localhost java]# java -version
java version "21.0.5" 2024-10-15 LTS
Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)

表示JDK21安装环境成功

可以输入 javac 再次进行验证

[root@localhost java]# javac
Usage: javac <options> <source files>
where possible options include:
  @<filename>                  Read options and filenames from file
  -Akey[=value]                Options to pass to annotation processors
  --add-modules <module>(,<module>)*
        Root modules to resolve in addition to the initial modules,
        or all modules on the module path if <module> is ALL-MODULE-PATH.
  --boot-class-path <path>, -bootclasspath <path>
        Override location of bootstrap class files
  --class-path <path>, -classpath <path>, -cp <path>
        Specify where to find user class files and annotation processors
  -d <directory>               Specify where to place generated class files
  -deprecation
        Output source locations where deprecated APIs are used
  --enable-preview
        Enable preview language features.
        To be used in conjunction with either -source or --release.
  -encoding <encoding>         Specify character encoding used by source files
  -endorseddirs <dirs>         Override location of endorsed standards path
  -extdirs <dirs>              Override location of installed extensions
  -g                           Generate all debugging info
  -g:{lines,vars,source}       Generate only some debugging info
  -g:none                      Generate no debugging info
  -h <directory>
        Specify where to place generated native header files
  --help, -help, -?            Print this help message
  --help-extra, -X             Print help on extra options
  -implicit:{none,class}
        Specify whether to generate class files for implicitly referenced files
  -J<flag>                     Pass <flag> directly to the runtime system
  --limit-modules <module>(,<module>)*
        Limit the universe of observable modules
  --module <module>(,<module>)*, -m <module>(,<module>)*
        Compile only the specified module(s), check timestamps
  --module-path <path>, -p <path>
        Specify where to find application modules
  --module-source-path <module-source-path>
        Specify where to find input source files for multiple modules
  --module-version <version>
        Specify version of modules that are being compiled
  -nowarn                      Generate no warnings
  -parameters
        Generate metadata for reflection on method parameters
  -proc:{none,only,full}
        Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]
        Names of the annotation processors to run;
        bypasses default discovery process
  --processor-module-path <path>
        Specify a module path where to find annotation processors
  --processor-path <path>, -processorpath <path>
        Specify where to find annotation processors
  -profile <profile>
        Check that API used is available in the specified profile.
        This option is deprecated and may be removed in a future release.
  --release <release>
        Compile for the specified Java SE release.
        Supported releases:
            8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
  -s <directory>               Specify where to place generated source files
  --source <release>, -source <release>
        Provide source compatibility with the specified Java SE release.
        Supported releases:
            8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
  --source-path <path>, -sourcepath <path>
        Specify where to find input source files
  --system <jdk>|none          Override location of system modules
  --target <release>, -target <release>
        Generate class files suitable for the specified Java SE release.
        Supported releases:
            8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
  --upgrade-module-path <path>
        Override location of upgradeable modules
  -verbose                     Output messages about what the compiler is doing
  --version, -version          Version information
  -Werror                      Terminate compilation if warnings occur

以上就是 linux 虚拟机安装 JDK21的操作过程

题外话:

免费建立了个测试群,免费分享一些资料,例如网课、面试题、简历模板等;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到