- Update System Packages:
First, ensure your system is up to date. Open a terminal and execute the following command:
shell
sudo yum update
shell2. **Check if Java is Installed:** Before installing the new Java SDK, verify whether Java is already installed on your system. Run the following command:
java -version
shellIf Java is installed, this command will display the current version. 3. **Download Java SDK:** Next, decide which Java SDK version to install—either from the Oracle website or OpenJDK. The example below uses OpenJDK. To install OpenJDK, use CentOS's package manager `yum`. For instance, to install OpenJDK 11, run:
sudo yum install java-11-openjdk-devel
shellIf you prefer Oracle JDK, download it from the Oracle website. Due to licensing requirements, you may need to accept the license agreement and register before downloading. 4. **Set Environment Variables:** To run Java and Javac from any location, configure the JAVA_HOME environment variable. First, identify the Java installation path:
sudo update-alternatives --config java
shellNote the installation path. Then, open `/etc/profile` or your user's configuration files (e.g., `.bash_profile`, `.bashrc`, or `.zshrc`), and add:
export JAVA_HOME=/path/to/your/java export PATH=$PATH:$JAVA_HOME/bin
shellReplace `/path/to/your/java` with the actual path found earlier. 5. **Verify Installation:** After saving and closing the file, reload the configuration or restart your terminal. Then run these commands to confirm successful installation and configuration:
java -version javac -version
shellBoth commands should return the installed Java version. The steps above provide the basic process for installing Java SDK on CentOS. If you require a specific Oracle JDK version or have unique configuration needs, the process may differ slightly.
2024年6月29日 12:07 回复