Please enable Javascript to view the contents

CentOS 7 安装 Java 开发环境

 ·  ☕ 1 分钟

1. 编译报错:java.nio.file.NoSuchFileException

在编译 Jenkins 插件时,提示错误信息如下:

1
2
3
4
mvn package

Compilation failure
[ERROR] java.nio.file.NoSuchFileException: /root/java/target/classes/META-INF/annotations/hudson.Extension

原来 Maven 使用的是 JAVA_HOME 而不是 PATH,找不到 Java 运行环境导致,而且 Java 11 不行,Java 1.8 才能正常编译。

2. 安装 JDK

  • 安装 Java 11
1
yum install -y java-11-openjdk-devel
  • 安装 Java 1.8
1
yum install -y java-1.8.0-openjdk-devel

3. 选择 JDK 版本

选择当前 JDK 版本:

1
2
3
4
5
6
7
8
alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/jre/bin/java)

选择 2 之后, /usr/lib/jvm/java-openjdk 即为当前选择的版本。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ls -l /usr/lib/jvm

lrwxrwxrwx 1 root root   26 Jul  1 15:42 java -> /etc/alternatives/java_sdk
lrwxrwxrwx 1 root root   29 Jul  1 15:42 java-11 -> /etc/alternatives/java_sdk_11
lrwxrwxrwx 1 root root   37 Jul  1 15:42 java-11-openjdk -> /etc/alternatives/java_sdk_11_openjdk
drwxr-xr-x 8 root root 4096 Jul  1 15:42 java-11-openjdk-11.0.7.10-4.el7_8.x86_64
lrwxrwxrwx 1 root root   34 Jul  1 15:42 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx 1 root root   21 Jul  1 16:01 jre -> /etc/alternatives/jre
lrwxrwxrwx 1 root root   24 Jul  1 15:42 jre-11 -> /etc/alternatives/jre_11
lrwxrwxrwx 1 root root   32 Jul  1 15:42 jre-11-openjdk -> /etc/alternatives/jre_11_openjdk
lrwxrwxrwx 1 root root   40 Jul  1 15:42 jre-11-openjdk-11.0.7.10-4.el7_8.x86_64 -> java-11-openjdk-11.0.7.10-4.el7_8.x86_64
lrwxrwxrwx 1 root root   29 Jul  1 15:42 jre-openjdk -> /etc/alternatives/jre_openjdk

查看 Java 版本:

1
2
3
4
5
java -version

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode

4. 配置环境变量

编辑文件

1
vim /etc/profile

添加环境变量

1
2
export JAVA_HOME=/usr/lib/jvm/java-openjdk
export JRE_HOME=/usr/lib/jvm/jre-openjdk

使环境变量生效

1
source /etc/profile

5. 卸载 JDK

查看当前环境安装的 JDK :

1
2
3
4
5
6
7
8
9
rpm -aq | grep -i jdk

copy-jdk-configs-3.3-10.el7_5.noarch
java-1.8.0-openjdk-headless-1.8.0.252.b09-2.el7_8.x86_64
java-11-openjdk-headless-11.0.7.10-4.el7_8.x86_64
java-11-openjdk-devel-11.0.7.10-4.el7_8.x86_64
java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64
java-11-openjdk-11.0.7.10-4.el7_8.x86_64
java-1.8.0-openjdk-devel-1.8.0.252.b09-2.el7_8.x86_64

不需要一个一个删除,只需要删除 copy-jdk-configs 即可。

1
yum remove -y copy-jdk-configs-3.3-10.el7_5.noarch

微信公众号
作者
微信公众号