Java is one of the most widely used object-oriented programming languages in the world today. Its popularity continues to grow rapidly, as acknowledged by Oracle. Companies of varying sizes leverage Java for diverse applications, including but not limited to banking software, device drivers, embedded systems, healthcare software, industrial automation control systems, weather stations, and e-commerce platforms.
The object-oriented nature of Java allows developers to treat everything as an object, unlike other structured languages like C++, where specific coding instructions are necessary. This simplicity, combined with its procedural-oriented capabilities, makes Java a versatile and powerful language.
The Java Development Kit (JDK) enables the creation of both Java applications and applets that can run on multiple operating systems. As one of the three core software programs for Java development, it is essential for compiling and executing Java code.
Installing Java on Linux can sometimes be challenging, but this guide offers straightforward instructions for installing Java 17 on Debian 11, ensuring that your system will be up and running efficiently in no time.
Prerequisites
To install and run Java programs on Linux, ensure you have:
- A server running Debian 11 with an active Internet connection.
- At least 3.5 GB of available RAM for smooth system performance.
- A non-root user with sudo privileges.
Step 1. Update the System
Begin by ensuring your system is up-to-date. Run the following command in the terminal:
sudo apt update && sudo apt upgrade -y
This command may take a few minutes depending on your Internet speed.
Step 2. Install Java JDK 17 on Debian 11
Before proceeding with the installation, verify if Java is already installed:
java -version
If Java isn’t installed, you will see output indicating its absence. Otherwise, a version number will be displayed.
Debian 11’s default repository includes Java 17. To confirm this, run:
sudo apt-cache search openjdk | grep 17
Proceed with installing both JDK and JRE to ensure you can run Java applications and applets:
sudo apt install openjdk-17-jdk
sudo apt install openjdk-17-jre
Verify the installation by checking the version:
java -version
Also, verify the JDK by checking the Java compiler version:
javac -version
Step 3. Configure the Environmental Variable
Some Java applications require the JAVA_HOME environmental variable. Here’s how to set it:
Identify the Java installation path using:
sudo update-alternatives --config java
- /usr/lib/jvm/java-11-openjdk-amd64/bin/java
- /usr/lib/jvm/java-17-openjdk-amd64/bin/java
Copy the desired path and create a system-wide environment variable using nano:
sudo nano /etc/environment
Add the following line at the end:
export JAVA_HOME=copied path
Save and exit: CTRL+X, Y, Enter.
Apply the change with:
source /etc/environment
Confirm the change:
echo $JAVA_HOME
Step 4. Test the Java Installation
Verify the Java setup by compiling and running a simple “Hello World” program:
Create and navigate to a new directory:
mkdir helloworld && cd helloworld
Create a new Java file using nano:
sudo nano helloworld.java
Add the following code:
public class helloworld { public static void main(String[] args) { System.out.println("Howtoforge-Hello World"); } }
Compile the code:
javac helloworld.java
Execute the program:
java helloworld
An output of “Howtoforge-Hello World” confirms success:
Congratulations, Java is successfully installed. For more, refer to the official Java documentation.
Conclusion
You have successfully installed Java 17 on Debian 11. If you have any feedback or questions, feel free to comment below. Share this guide with friends and colleagues if you found it helpful!
FAQ
Q: Can I install a different version of Java?
A: Yes, you can install other JDK versions by replacing “openjdk-17” with your desired version in the installation command.
Q: What should I do if JAVA_HOME is not set correctly?
A: Ensure the path you copied is correct and follow the environmental variable setup steps again.
Q: How can I uninstall Java from my system?
A: Use the package manager with the command sudo apt remove openjdk-17-jdk openjdk-17-jre
to remove Java from your system.