第03章 CentOS7 中 JDK1.8 的安装与配置

本文详细介绍在CentOS7系统中安装与配置JDK的过程,包括下载JDK、基于tar.gz和RPM文件的安装步骤,以及配置JAVA环境变量的方法。适合初学者快速上手。

第03章 CentOS7 中 JDK 的安装与配置

— 大神的视角,码农的温度 —

全文 2299 字,菜鸟18分钟,初级10分钟,中级5分钟,大神请绕道。

文档目的

  • 在 CentOS 系统中完成 JDK 的安装。
  • 在 CentOS 系统中配置 JAVA 环境变量。
  • 为后续架构和开发提供最基础的 JAVA 环境支持。

3.1 下载 JDK

基于当前 JDK 的版本和企业常用版本情况,选择 JDK 1.8 作为主要开发工具。

JDK 1.8 下载地址

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

JDK1.8 官方文档:

https://docs.oracle.com/javase/8/index.html

3.2 安装 JDK

3.2.1 基于 tar.gz 文件在 Linux平台上的安装步骤

新建 jdk8 目录,进入目录下载安装文件,解压安装

## 接受许可的下载地址,也可以下载好通过 vsftpd 上传到虚拟机
[root@localhost ~]# wget http://download.oracle.com/otn-pub/java/jdk/8u172-b11/a58eab1ec242421181065cdc37240b08/jdk-8u172-linux-x64.tar.gz?AuthParam=1531118597_9ab34b86d1582abc9efbf22b9dc283d3  
## 重命名一下
[root@localhost ~]# mv jdk-8u172-linux-x64.tar.gz?AuthParam=1531118597_9ab34b86d1582abc9efbf22b9dc283d3 jdk-8u172-linux-x64.tar.gz 
## 解压安装,这里是通过 vsftpd 上传的
[root@localhost ~]# tar zxvf /home/vsftpd/kony/jdk-8u172-linux-x64.tar.gz -C /usr/bin/java/

下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

https://download.oracle.com/otn/java/jdk/8u261-b12/a4634525489241b9a9e1aa73d9e118e6/jdk-8u261-linux-x64.tar.gz?AuthParam=1598692449_779b21aa75c9b81940429f5c131b3716

以下是官方文档中提供的安装步骤:

Installation of the 64-bit JDK on Linux Platforms

This procedure installs the Java Development Kit (JDK) for 64-bit Linux, using an archive binary file (.tar.gz).

These instructions use the following file:

jdk-8u*version*-linux-x64.tar.gz

1、Download the file.

Before the file can be downloaded, you must accept the license agreement. The archive binary can be installed by anyone (not only root users), in any location that you can write to. However, only the root user can install the JDK into the system location.

2、Change directory to the location where you would like the JDK to be installed, then move the .tar.gz archive binary to the current directory.

3、Unpack the tarball and install the JDK.

% tar zxvf jdk-8uversion-linux-x64.tar.gz

The Java Development Kit files are installed in a directory called jdk1.8.0_``version in the current directory.

4、Delete the .tar.gz file if you want to save disk space.

3.2.2 基于 RPM 在 Linux平台上的安装步骤

Installation of the 64-bit JDK on RPM-based Linux Platforms

This procedure installs the Java Development Kit (JDK) for 64-bit RPM-based Linux platforms, such as Red Hat and SuSE, using an RPM binary file (.rpm) in the system location. You must be root to perform this installation. These instructions use the following file:

jdk-8u*version*-linux-x64.rpm

1、Download the file.

Before the file can be downloaded, you must accept the license agreement.

2、Become root by running su and entering the super-user password.

3、Uninstall any earlier installations of the JDK packages.

# rpm -e *package_name*

4、Install the package.

# rpm -ivh jdk-8uversion-linux-x64.rpm

To upgrade a package:

# rpm -Uvh jdk-8uversion-linux-x64.rpm

5、Delete the .rpm file if you want to save disk space.

6、Exit the root shell. No need to reboot.

Starting with version 8u40, the JDK installation is integrated with the alternatives framework and after installation, the alternatives framework is updated to reflect the binaries from the recently installed JDK. Java commands such as java, javac, javadoc, and javap can be invoked from the command line.

Using the java -version command, users can confirm the default (recently installed) JDK version.

In addition, users can now check which specific RPM package provides the java files:

rpm -q --whatprovides java

这里就不翻译了,英文不行的可以借助 Google 翻译,以上是直接来自官方文档的安装步骤,其实我的英文很渣渣渣,是属于只能硬着头皮上的那种。

3.2.3 JDK安装资料推荐

推荐参考官方文档,文档中有非常详细的基于不同平台,提供了不同的安装步骤说明。

官方安装手册地址:

https://docs.oracle.com/javase/8/docs/technotes/guides/install/linux_jdk.html#BJFGGEFG

在 Linux 平台中安装64位 JDK1.8文档地址:

https://docs.oracle.com/javase/8/docs/technotes/guides/install/linux_jdk.html#BJFJJEFG

推荐码农自己看官方文档,要想成为大神&精英,必须强制自己看。

3.3 Java 环境变量配置

解压安装,将下载的JDK文件解压到指定目录:

# tar zxvf jdk-8u261-linux-x64.tar.gz -C /usr/local/

从 jdk8 进入到已解压的目录,开始配置 JDK 环境变量

[root@localhost jdk1.8.0_261]# ll   ## 查看解压目录文件结构
[root@localhost jdk1.8.0_261]# pwd  ## 查看当前路径
/usr/bin/java/jdk1.8.0_261         ## 具体的解压目录
[root@localhost jdk1.8.0_261]# vi ~/.bashrc ##编辑配置文件

编辑根目录下的.bashrc配置文件, i键进入编辑模式, 在结尾加入以下内容

export JAVA_HOME=/usr/local/jdk1.8.0_261
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

具体路径按照自己实际路径修改, 编辑完成之后, Esc键退出编辑, 输入:x,然后Enter, 保存并退出。

## 运行source ~/.bashrc, 使修改生效
[root@localhost java]# source ~/.bashrc

运行java -version查看java版本, 验证环境变量是否配置完成

[root@localhost jdk1.8.0_172]# java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

至此,在 CentOS 7系统中 jdk 环境变量配置已完成。

3.4 集群jdk配置

确保与其他机器网络相通,并同时都需要安装

# yum install openssh-clients -y

拷贝文件,文件传输

# scp /opt/java/jdk-8u261-linux-x64.tar.gz root@192.168.111.138:/opt/
root@192.168.111.136's password: 

重复3.3步骤即可快速配置其他集群机器,减少文件的下载

软链接的创建,删除,修改

Linux软链接,类似于windows系统的快捷键。譬如你将windows系统的D盘中某一个文件夹放在桌面上当做快捷键。

一、软链接创建

1、创建软链接

  1. #ln -s 【目标目录】 【软链接地址】
    【目标目录】指软连接指向的目标目录下,【软链接地址】指“快捷键”文件名称,该文件是被指令创建的。如下示例,public文件本来在data文件下是不存在的,执行指令后才存在的。
  2. 软链接创建需要同级目录下没有同名的文件。就像你在windows系统桌面创建快捷键时,不能有同名的文件。
    当同级目录下,有同名的文件存在时,会报错误。

二、删除

  1. #rm -rf 【软链接地址】
  2. 上述指令中,软链接地址最后不能含有“/”,当含有“/”时,删除的是软链接目标目录下的资源,而不是软链接本身。

三、修改

  1. #ln -snf 【新目标目录】 【软链接地址】
  2. 这里修改是指修改软链接的目标目录 。

ows系统桌面创建快捷键时,不能有同名的文件。
当同级目录下,有同名的文件存在时,会报错误。

二、删除

  1. #rm -rf 【软链接地址】
  2. 上述指令中,软链接地址最后不能含有“/”,当含有“/”时,删除的是软链接目标目录下的资源,而不是软链接本身。

三、修改

  1. #ln -snf 【新目标目录】 【软链接地址】
  2. 这里修改是指修改软链接的目标目录 。

3.5 小结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值