introduction
In modern software development, Java is a widely used programming language, and its version is updated frequently. Different projects may rely on different versions of Java running environments. As a popular Linux distribution, CentOS is often used in server deployment and development environments. This article will introduce in detail how to install Java 17 on CentOS and realize multiple versions of coexist with existing Java 8 to ensure that different projects can run smoothly.
1. Introduction to CentOS System
CentOS is an open source operating system based on Red Hat Enterprise Linux (RHEL), and is widely popular for its stability, security and compatibility. It is suitable for server environments and supports the deployment of a variety of software and services.
2. The importance of Java 17
Java 17 is a long-term support (LTS) version of the Java platform, introducing many new features and improvements, such as pattern matching, sealing classes, and new garbage collectors. These features make Java 17 an ideal choice for developing high-performance, modern applications.
3. How to install Java 17
There are several ways to install Java 17 on CentOS, including usingyum
Package manager, manual download of installation packages, etc. Here are the detailed steps:
(I) Update the system software package
Before installing Java 17, it is recommended to update the system package to ensure system stability and compatibility:
sudo yum update -y
(II) Install Java 17
CentOS defaultyum
Java 17 may not be included in the repository, so additional repositories need to be enabled. Here are the installation steps:
1. Enable EPEL repository
sudo yum install -y epel-release
2. Install OpenJDK 17
sudo yum install -y java-17-openjdk-devel
ifyum
Prompt not foundjava-17-openjdk-devel
Package, you can try to manually download and install it.
(III) Manually download and install OpenJDK 17
ifyum
The Java 17 package cannot be found, and OpenJDK 17 can be downloaded and installed manually.
1. Download OpenJDK 17
Download the file for OpenJDK 17 from AdoptOpenJDK or other trusted sources:
wget /adoptium/temurin17-binaries/releases/download/jdk-17.0.8.1+1/OpenJDK17U-jdk_x64_linux_hotspot_17.0.8.1_1.
2. Unzip to the specified directory
Unzip the downloaded file to/usr/local/
Or other suitable directory:
sudo tar xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.8.1_1. -C /usr/local/
3. Configure environment variables
edit/etc/profile
File, add the following content:
export JAVA_HOME=/usr/local/jdk-17.0.8.1+1 export PATH=$JAVA_HOME/bin:$PATH
Then make the environment variable take effect:
source /etc/profile
4. Verify the installation
Check if the Java version is correct:
java -version
The output should display the version information of OpenJDK 17.
4. Configure multi-version Java environment
On CentOS, you can useupdate-alternatives
Tools to manage multiple Java versions and switch as needed.
(I) Add Java version to update-alternatives
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-17-openjdk/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-17-openjdk/bin/javac 1
(II) Switch the Java version
sudo update-alternatives --config java
Select the corresponding Java version number in the pop-up menu.
(III) Set up Java version for specific projects
If you don't want to switch the Java version globally, you can specify the Java version in your project.
Maven
existSpecify the Java version in :
<properties> <>17</> <>17</> </properties>
Gradle
existSpecify the Java version in :
sourceCompatibility = '17' targetCompatibility = '17'
Environment variables
Set up for specific projectsJAVA_HOME
:
export JAVA_HOME=/usr/local/jdk-17 export PATH=$JAVA_HOME/bin:$PATH
5. Frequently Asked Questions and Solutions
There are some common problems that may occur during the installation and configuration of Java 17. Here is the solution:
(1) yum prompts that Java 17 package cannot be found
ifyum
Prompt not foundjava-17-openjdk-devel
Package, you can try to manually download and install it.
(II) Update-alternatives configuration failed
ifupdate-alternatives
Configuration failed, you can edit it manually/etc/profile
File, settingsJAVA_HOME
andPATH
。
(III) The Java version cannot take effect after switching
If the Java version does not take effect, you can try restarting the system or reloading the environment variables:
source /etc/profile
6. Summary
Installing Java 17 on CentOS and implementing multi-version coexistence is a relatively simple process. Java 17 can be easily installed by using the yum package manager or by manually downloading the installation package. At the same time, using the update-alternatives tool, you can easily switch between different versions of Java to meet the needs of different projects.
The above is the detailed tutorial on installing Java 17 on CentOS and realizing multi-version coexistence. For more information on installing Java 17 on CentOS and coexistence of multiple versions, please pay attention to my other related articles!