Java, as a leading programming language, powers a vast array of applications. For developers working on Linux systems, knowing the Java version installed is crucial. One of the quickest ways to check the Java version is by using the java -version command, which provides immediate feedback on the currently installed Java Runtime Environment (JRE). This makes our work streamlined and saves us from compatibility issues.

Each Linux distribution might have its quirks. On popular systems like Ubuntu, Debian, and CentOS, alternative methods such as using update-alternatives --config java or checking through package managers (apt, dpkg, yum, or dnf) offer flexibility. This is particularly useful for environments where multiple Java Development Kits (JDKs) coexist, giving us precise control and clarity over our development setup.
No matter if we are troubleshooting or simply verifying our setup, understanding these commands becomes a developer’s second nature. From finding out if we’re running Oracle JDK or OpenJDK to ensuring compatibility across our projects, these little checks can significantly ease our Linux journey. Ready to dive deeper? Let’s explore these techniques and ensure our Java environment is just as we need it!
Contents
Setting Up Java on Linux Systems
Setting up Java on a Linux system involves determining if Java is already installed, installing it using package managers, and configuring the environment for your needs. These steps make sure Java runs correctly and efficiently.
Determining Java Installation Status
First, we need to check if Java is installed on our system. This can be done quickly with the java -version command in the terminal. Here’s how:
- Open Terminal: You can usually do this by pressing
Ctrl + Alt + Tor searching for “Terminal” in your application launcher. - Run the Command: Type
java -versionand pressEnter.
If Java is installed, the installed version will be displayed. If you see a message like java: command not found, it means Java is not installed.
Tip: It’s always good to check if OpenJDK or Oracle JDK is installed, as the results may vary slightly.
Installing Java with Package Managers
If Java is not installed, we can install it using package managers like apt or yum. The process is simple and ensures we get the necessary JRE or JVM for our needs.
-
For Debian/Ubuntu:
sudo apt update sudo apt install default-jdk -
For CentOS/RHEL:
sudo yum install java-11-openjdk-devel -
For Fedora:
sudo dnf install java-11-openjdk-devel
These commands will install OpenJDK 11, which is a widely used version. You can specify different versions if needed, depending on your project requirements.
Configuring the Java Environment
After installation, configuring the Java environment ensures smooth operation. First, we set the JAVA_HOME environment variable, which many applications rely on:
-
Find Java’s Installation Path:
sudo update-alternatives --config java -
Set
JAVA_HOME:
Add the following lines to your.bashrcor.profilefile:export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 export PATH=$PATH:$JAVA_HOME/bin -
Apply Changes:
source ~/.bashrc
Make sure to replace the path with the one that matches your installation. This configuration ensures that our system correctly locates and uses the installed Java version.
Note: Different Linux distributions might have slight variations in paths, so always verify the correct path for your setup.
Managing Java Versions
Managing Java versions efficiently is essential to ensure stable software performance and security. Let’s discuss how to check the current Java version, switch between multiple installations, and update to the latest versions accurately.
Checking the Current Java Version
To determine the Java version installed on our system, we use the java -version command. This command will display the installed Java version along with additional information about the runtime environment. Here’s what you typically see:
$ java -version
This output provides details about the major, minor, and security versions of Java. For instance, if Java 11 is installed, you might see something like:
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10)
OpenJDK 64-Bit Server VM (build 11.0.8+10, mixed mode)
This output confirms Java 11 is the default. If the command returns “java: command not found,” it means Java isn’t installed.
Switching Between Multiple Java Installations
If we have multiple Java versions installed, we often need to switch between them. We can manage these installations using the update-alternatives command. Here’s a quick way to list all available Java installations:
$ sudo update-alternatives --config java
This command shows a list of installed Java versions, from which we can choose the default. Selecting the desired version is a simple task:
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1081 manual mode
2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
3 /usr/lib/jvm/java-13-openjdk-amd64/bin/java 1131 manual mode
Press <enter> to keep the current choice[*], or type selection number:
By selecting the appropriate version number, we can control the default Java version for our system.
Updating Java to the Latest Version
Maintaining up-to-date Java versions is crucial for performance improvements and security patches. To update Java, we need to first check for available updates using our package manager. For Debian-based systems (like Ubuntu), we use:
$ sudo apt update
$ sudo apt install openjdk-11-jdk
For Arch Linux and its derivatives, execute:
$ sudo pacman -Syu
$ sudo pacman -S jdk-openjdk
After the installation, verify the update:
$ java -version
This will ensure that our system is using the latest Java version available. Regularly checking for updates and installing them helps us avoid potential security vulnerabilities and take advantage of new features.
Utilizing Java Tools for Development
When developing in Java, there are several essential tools we rely on to compile code and manage our project’s dependencies.
Compiling Code with Javac
The Java compiler, javac, is fundamental for converting our Java source code into bytecode. We simply use the command line to execute the javac command, as follows:
javac MyClass.java
This command compiles the MyClass.java file, generating a MyClass.class file. Additionally, we can retrieve the compiler’s version with:
javac -version
This helps us ensure we’re working with the latest bug fixes and security patches.
For developers aiming to compile multiple files or entire directories, the use of wildcards and directories can streamline the process:
javac *.java
In integrated development environments (IDEs) with GUI, such as Eclipse or IntelliJ IDEA, the compilation process is often automated, but knowledge of command line javac usage remains crucial when working on servers or environments without GUIs.
Managing Packages and Libraries
Managing packages and libraries is crucial for keeping a Java project organized and efficient. We use package declarations at the top of our Java files:
package com.myproject.utilities;
This organizes our classes into namespaces, making large projects more manageable.
For external libraries, tools like Maven or Gradle handle dependencies. We define our required libraries in a configuration file (like pom.xml for Maven) and these tools manage the downloads and updates for us.
Here’s how a basic dependency entry in pom.xml looks:
<dependency>
<groupId>org.example</groupId>
<artifactId>example-lib</artifactId>
<version>1.0.0</version>
</dependency>
These tools also help us integrate new features and security patches into our projects, ensuring we maintain best practices in Java development. By managing dependencies centrally, we avoid conflicts and streamline the development process.
| Tool | Purpose | Example Command |
| javac | Compile Java code | `javac MyClass.java` |
| Maven | Manage dependencies | Define in `pom.xml` |
| Gradle | Manage dependencies | Define in `build.gradle` |
These tools are essential for leveraging the full power of Java in our projects.