Java Development Kit (JDK) is essential for developers working on Java applications. Ubuntu 24.04 LTS provides a straightforward way to install the default JDK, which ensures you have the necessary tools to develop and run Java applications. This guide will walk you through the process of installing the default JDK on Ubuntu 24.04 LTS.
Step 1: Update the Package Index
Before installing any package, it’s a good practice to ensure your system’s package index is up to date. Open your terminal and run:
sudo apt update
sudo apt upgrade -y
This will ensure you have the latest software updates and dependencies.
Step 2: Install Default JDK
Ubuntu repositories include a package named default-jdk
, which installs the most commonly used version of the JDK.
Run the following command to install the default JDK:
sudo apt install default-jdk -y
This command will install the OpenJDK version recommended for Ubuntu 24.04.
Step 3: Verify the Installation
Once the installation is complete, you can verify the JDK installation and check the installed version using the java
command:
java -version
The output should display the installed version of OpenJDK. For example:
openjdk version "17.0.8" 2024-09-24
OpenJDK Runtime Environment (build 17.0.8+10-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 17.0.8+10-Ubuntu-1, mixed mode, sharing)
Step 4: Set the JAVA_HOME Environment Variable
For some applications, you might need to set the JAVA_HOME
environment variable. Here’s how to do it:
- Open the environment configuration file:bashCopy code
sudo nano /etc/environment
- Add the following line to the file, replacing the path with the one where Java is installed:bashCopy code
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Save the file and exit. - Apply the changes:bashCopy code
source /etc/environment
- Verify the
JAVA_HOME
variable:bashCopy codeecho $JAVA_HOME
The output should display the Java installation path.
Step 5: Test the Installation
To ensure everything is set up correctly, you can compile and run a simple Java program:
1.Create a file named HelloWorld.java
:
nano HelloWorld.java
2.Add the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
3.Compile the program:
javac HelloWorld.java
4.Run the program:
java HelloWorld
You should see the output:
Hello, World!
Conclusion
You’ve successfully installed the default JDK on Ubuntu 24.04 LTS. With Java ready to go, you can now develop and run Java applications. If you encounter any issues or need a specific version of the JDK, consider exploring additional installation methods, such as downloading Oracle JDK or using SDKMAN.
Happy coding! 🚀