如何在CentOS服务器上安装Java JDK和JRE

时间:2020-02-23 14:30:43  来源:igfitidea点击:

在本教程中,我们将学习在centOS服务器上安装Java JDK和JRE。

前提条件

假设您已经安装了wget

在终端中使用以下命令来安装wget。

yum install -y wget

如果您没有权限,请使用sudo

步骤1下载JRE

在本教程中,我们将安装JRE(Java运行时环境)版本7。

打开终端并使用ssh登录到服务器。
现在,将cd转到您要下载文件的目录。

假设我们有一个" Downloads"目录,并且我们想在该文件夹中下载JRE。

在终端中执行以下命令以下载JRE版本7。

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm"
[~]# cd Downloads
[Downloads]# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm"
--2015-12-12 05:07:20--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm
Resolving download.oracle.com (download.oracle.com)... 96.17.72.58, 96.17.72.66
Connecting to download.oracle.com (download.oracle.com)|96.17.72.58|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm [following]
--2015-12-12 05:07:21--  https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm
Resolving edelivery.oracle.com (edelivery.oracle.com)... 2600:1413:1:500:8300::2d3e, 2600:1413:1:500:8800::2d3e, 23.67.2.41
Connecting to edelivery.oracle.com (edelivery.oracle.com)|2600:1413:1:500:8300::2d3e|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm?AuthParam=1490850561_4973818e7b56fb9dd787998708cf1ae2 [following]
--2015-12-12 05:07:21--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm?AuthParam=1490850561_4973818e7b56fb9dd787998708cf1ae2
Connecting to download.oracle.com (download.oracle.com)|96.17.72.58|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 33706160 (32M) [application/x-redhat-package-manager]
Saving to: ‘jre-7u79-linux-x64.rpm’

100%[==================================================================================================>] 33,706,160  3.52MB/s   in 9.1s   

2015-12-12 05:07:30 (3.52 MB/s) - ‘jre-7u79-linux-x64.rpm’ saved [33706160/33706160]

[Downloads]# ls -la
total 1028248
drwxr-xr-x  2 root root       4096 Apr 13 05:07 .
dr-xr-x---. 6 root root       4096 Apr 13 05:07 ..
-rw-r--r--  1 root root   33706160 Apr 13  2014 jre-7u79-linux-x64.rpm

步骤2安装JRE

在终端中使用以下命令来安装JRE。

yum install jre-7u79-linux-x64.rpm

如果您没有权限,请使用sudo

[Downloads]# sudo yum install jre-7u79-linux-x64.rpm
Loaded plugins: fastestmirror
Examining jre-7u79-linux-x64.rpm: jre-1.7.0_79-fcs.x86_64
Marking jre-7u79-linux-x64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package jre.x86_64 0:1.7.0_79-fcs will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                   Arch                         Version                             Repository                                 Size
============================================================================================================================================
Installing:
 jre                       x86_64                       1.7.0_79-fcs                        /jre-7u79-linux-x64                        81 M

Transaction Summary
============================================================================================================================================
Install  1 Package

Total size: 81 M
Installed size: 81 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jre-1.7.0_79-fcs.x86_64                                                                                                  1/1 
Unpacking JAR files...
	rt.jar...
	jsse.jar...
	charsets.jar...
	localedata.jar...
	jfxrt.jar...
  Verifying  : jre-1.7.0_79-fcs.x86_64                                                                                                  1/1 

Installed:
  jre.x86_64 0:1.7.0_79-fcs                                                                                                                 

Complete!
[Downloads]#

步骤3下载JDK

在本教程中,我们将下载并安装JDK(Java开发工具包)版本7。

使用以下命令下载JDK版本7。

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm"
[Downloads]# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm"
--2015-12-12 05:14:04--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm
Resolving download.oracle.com (download.oracle.com)... 96.17.72.66, 96.17.72.58
Connecting to download.oracle.com (download.oracle.com)|96.17.72.66|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm [following]
--2015-12-12 05:14:04--  https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm
Resolving edelivery.oracle.com (edelivery.oracle.com)... 2600:1413:1:500:8800::2d3e, 2600:1413:1:500:8300::2d3e, 23.67.2.41
Connecting to edelivery.oracle.com (edelivery.oracle.com)|2600:1413:1:500:8800::2d3e|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm?AuthParam=1490850965_eb153a76b759c1c9d185ee439e9de862 [following]
--2015-12-12 05:14:05--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm?AuthParam=1490850965_eb153a76b759c1c9d185ee439e9de862
Connecting to download.oracle.com (download.oracle.com)|96.17.72.66|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 138082565 (132M) [application/x-redhat-package-manager]
Saving to: ‘jdk-7u79-linux-x64.rpm’

100%[==================================================================================================>] 138,082,565 3.97MB/s   in 35s    

2015-12-12 05:14:41 (3.72 MB/s) - ‘jdk-7u79-linux-x64.rpm’ saved [138082565/138082565]

[Downloads]#

步骤4安装JDK

要安装JDK,请在终端中使用以下命令。

yum install jdk-7u79-linux-x64.rpm

如果您没有权限,请使用sudo

[Downloads]# sudo yum install jdk-7u79-linux-x64.rpm
Loaded plugins: fastestmirror
Examining jdk-7u79-linux-x64.rpm: 2000:jdk-1.7.0_79-fcs.x86_64
Marking jdk-7u79-linux-x64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package jdk.x86_64 2000:1.7.0_79-fcs will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                  Arch                        Version                                Repository                                Size
============================================================================================================================================
Installing:
 jdk                      x86_64                      2000:1.7.0_79-fcs                      /jdk-7u79-linux-x64                      209 M

Transaction Summary
============================================================================================================================================
Install  1 Package

Total size: 209 M
Installed size: 209 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2000:jdk-1.7.0_79-fcs.x86_64                                                                                             1/1 
Unpacking JAR files...
	rt.jar...
	jsse.jar...
	charsets.jar...
	tools.jar...
	localedata.jar...
	jfxrt.jar...
  Verifying  : 2000:jdk-1.7.0_79-fcs.x86_64                                                                                             1/1 

Installed:
  jdk.x86_64 2000:1.7.0_79-fcs                                                                                                              

Complete!
[Downloads]#

步骤5设置环境

现在,打开.bash_profile文件并设置JAVA_HOMEJRE_HOME

[Downloads]# cd
[~]# ls -la
total 96
dr-xr-x---.  8 root root  4096 Dec 31 20:15 .
drwxr-xr-x  18 root root  4096 Dec 29 06:15 ..
-rw-------.  1 root root 11146 Dec 31 20:39 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--   1 root root   429 Dec 31 10:38 .bash_profile

使用vi或者vim打开.bash_profile文件。

[~]# vi .bash_profile

现在添加以下行。
按i键切换到INSERT模式。

export JAVA_HOME=/usr/java/jdk1.7.0_79
export JRE_HOME=/usr/java/jdk.7.0_79/jre

PATH=$HOME/bin:$JAVA_HOME/bin:$JRE_HOME:$PATH

按Esc键退出INSERT模式。
现在输入:wq保存更改并退出。

现在,使用source命令加载更改。

[~]# source .bash_profile

所有设置完毕,现在通过在终端中执行以下命令来检查Java版本。

[~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)