For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
If you're diving into the world of Java development or looking to run powerful Java-based applications, mastering the installation process is your first big step.
Java continues to dominate the programming world with its versatility, reliability, and massive community support. Whether you're a curious beginner or an experienced developer setting up a new environment, learning to install Java in Linux is a crucial milestone.
In this guide, we’ll take you through every detail—from choosing the right Java version to getting your first program up and running smoothly on your Linux system.
Kickstart your journey in Java and software development with this industry-recognized Software Engineering program.
The Java Runtime Environment (JRE), the Java Virtual Machine (JVM), and the Java Development Kit (JDK) are among the components of Java. A JDK is necessary for the development and execution of Java programs, while a JRE is only needed for execution.
The JVM is responsible for translating and executing the Java bytecode that the JRE receives. Thus, understanding the differences between JDK, JRE, and JVM before learning how to install Java in Linux OS is crucial.
Several Java Development Kit(JDK) versions can be installed on different operating systems, including Linux, Windows, and Mac OS. The following is a detailed tutorial on how to install Java in a Linux terminal.
Boost your development career with these top-rated tech programs:
• UpGrads Full Stack Development Bootcamp
• AI-Powered Full Stack Development by IIIT Bangalore
• UpGrads Cloud Computing & DevOps Certificate
Before you start installing Java, it’s essential to know exactly what you’re setting up.
Java isn’t just a single package—it comes with different components, each serving a specific purpose. Understanding the difference between the JVM, JRE, and JDK will help you choose the right one based on whether you want to run or develop Java applications.
The Java Virtual Machine, or JVM, is the core engine that makes Java applications portable and platform-independent. It takes the compiled Java bytecode (.class files) and translates it into machine code that your computer can execute.
However, the JVM by itself is purely for running programs — it doesn’t include tools for writing, compiling, or debugging Java code. Think of the JVM as the interpreter that allows your system to understand and execute Java applications, but not to create them.
In short, if you only have the JVM installed, you can run Java programs that others have created, but you won’t be able to build or modify Java applications yourself.
Check out: Exploring Java Architecture: A Guide to Java's Core, JVM and JDK Architecture
The Java Runtime Environment, or JRE, builds on top of the JVM. It bundles the JVM together with a set of libraries, Java class files, and other necessary resources that Java programs need to function properly.
Without the JRE, even if you have the JVM, many Java applications would not run because they rely on these libraries and support files.
In short, the JRE allows you to run Java applications smoothly, but like the JVM, it does not include the development tools you would need to write or compile new programs. It's perfect for end-users who only want to execute Java apps without developing anything themselves.
Must explore: How Do Java Programs Work?
The Java Development Kit, or JDK, is the complete package for anyone who wants to develop Java applications.
It includes everything found in the JRE (meaning you can run Java programs), but it also adds essential development tools such as the Java compiler (javac), a debugger (jdb), and various utilities for documentation (javadoc) and packaging.
If you’re planning to write, compile, test, or debug Java programs, the JDK is absolutely necessary.
In short, if you're a developer (or learning to be one), you need the JDK. It provides everything required to create and run Java programs from scratch.
Also read: How to Install Java in Windows 10
Feature/Aspect | JRE (Java Runtime Environment) | OpenJDK | Oracle JDK |
Purpose | Run existing Java applications | Develop and run Java applications | Develop and run Java applications |
Includes Development Tools | No (Only runtime environment) | Yes (Compiler, debugger, etc.) | Yes (Compiler, debugger, additional tools) |
License | Free for personal and commercial use | Open-source (GNU General Public License - GPL) | Free for personal use; Subscription needed for commercial use |
Support & Updates | Limited | Community-driven updates and support | Oracle-provided updates, patches, and long-term support (LTS) |
Performance | Optimized for running Java apps | Very good; matches Oracle JDK closely | Highly optimized with some enterprise enhancements |
Additional Features | None beyond running applications | Standard Java SE features | Advanced monitoring, flight recorder, mission control tools |
Suitability For | Users who only want to run Java applications | Developers and general production environments | Enterprises needing long-term support and additional features |
Cost | Free | Free | Requires payment/subscription for commercial use |
Also check: Why is Java a Platform Independent Language?
Before installing Java, it’s a good idea to check whether it's already installed on your system. Here’s how you can do it:
Type the following command and press Enter:java -version
OpenJDK Runtime Environment (build 21.0.2+13)
OpenJDK 64-Bit Server VM (build 21.0.2+13, mixed mode)
To verify if the Java Development Kit (JDK) is installed (not just the runtime), check for the compiler:
javac -version
Using your Linux distribution package administration feature is one of the simplest methods to get Java on your computer and install it. The primary steps are as follows:
Step 1: Open a window on your Linux Terminal.
Step 2: Run the command below to update the package list as follows:
$ sudo apt update
Step 3: The following command should be executed when installing the default Java Runtime Environment (JRE):
$ sudo apt install default-jr
The default JRE for your system will be installed with this command.
Step 4: Check the Java version on your machine using the command below to confirm the installation:
$ java -version
We installed and downloaded Java on the Linux machine using the package manager.
If you need the JDK for Java development, here’s how to install it:
Install OpenJDK JDK
On Ubuntu/Debian:
sudo apt update
sudo apt install default-jdk
Or install a specific version, like OpenJDK 21:
sudo apt install openjdk-21-jdk
On CentOS/RHEL/Fedora:
sudo dnf install java-21-openjdk-devel
Check your installed version:
java -version
javac -version
Both should show the Java version if the installation was successful.
An IDE (Integrated Development Environment) makes writing, debugging, and running Java programs easier.
Popular Java IDEs available for Linux include:
Install Eclipse on Ubuntu/Debian:
sudo snap install --classic eclipse
sudo snap install intellij-idea-community --classic
Or, download directly from the official websites and follow the Linux installation guides provided.
Now that everything’s ready, let’s write and run your first Java program!
nano HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Linux Java World!");
}
}
Save and exit the file (in nano, press CTRL+O, Enter, then CTRL+X).
javac HelloWorld.java
This will generate a HelloWorld.class file.
java HelloWorld
Output:
Hello, Linux Java World!
Congratulations! You've successfully compiled and run your first Java program on Linux.
Installing Java on Linux is straightforward once you understand the different components—JRE, JDK, and OpenJDK vs Oracle JDK.
Whether you are setting up a development machine or just need Java to run applications, Linux offers a flexible, easy installation experience through package managers or manual downloads.
After setting up Java and an IDE, you’re ready to dive deep into the world of Java programming.
If you encounter any issues during installation, Linux communities and official Java documentation are great places for troubleshooting help.
Happy coding!
1. What version of Java should I install on Linux?
The version of Java to install depends on your specific needs. If you are just running applications, the JRE will suffice, but for development, you need the JDK. Most users opt for OpenJDK, which is free and open-source.
2. Can I install Java on Linux via a package manager?
Yes, Linux makes it easy to install Java using its package managers like apt for Ubuntu/Debian or dnf for Fedora. This ensures that you install a version compatible with your system and automatically manages dependencies. Using the package manager is the most efficient way to install Java.
3. How do I check if Java is installed on my Linux system?
You can check if Java is installed by typing java -version or javac -version in the terminal. If Java is installed, the terminal will display the version information. If it's not installed, you'll receive an error, indicating you need to install it.
4. Is OpenJDK as good as Oracle JDK?
OpenJDK is a fully open-source implementation of Java and is widely used in both development and production environments. It provides everything you need for Java development and is suitable for most users. While Oracle JDK has some additional features and support, OpenJDK is often sufficient for most Linux users.
5. How do I install the latest version of Java on Linux?
To install the latest version of Java, you can either use the package manager with a command like sudo apt install openjdk-21-jdk or download it directly from the official Oracle or OpenJDK websites. Always make sure to check for updates and install the latest stable version for security and performance.
6. Can I install multiple versions of Java on Linux?
Yes, Linux allows you to install multiple versions of Java simultaneously. You can configure your system to use a specific version of Java for different projects, making it easy to manage multiple environments. Use the update-alternatives command on Ubuntu to switch between Java versions.
7. How do I set Java environment variables on Linux?
You need to set JAVA_HOME and update your PATH variable for Java to work properly. You can add these variables to your .bashrc or .profile file to make them persistent. After updating the file, run source ~/.bashrc to apply the changes.
8. What IDEs are recommended for Java development on Linux?
Popular Java IDEs for Linux include Eclipse, IntelliJ IDEA, and NetBeans. These IDEs offer robust features like code suggestions, debugging, and integrated build systems. Most of them are available for free, with advanced versions offering additional features for enterprise use.
9. How do I compile and run a Java program on Linux?
To compile a Java program, use the javac command followed by the filename (e.g., javac HelloWorld.java). After compilation, run the program using the java command (e.g., java HelloWorld). Make sure your Java files are located in the same directory where you're running these commands.
10. How do I uninstall Java from Linux?
Uninstalling Java from Linux is simple with the package manager. For example, use sudo apt remove openjdk-21-jdk on Ubuntu/Debian, or sudo dnf remove java-21-openjdk on Fedora. You can also use sudo apt autoremove to remove unnecessary dependencies.
11. Why is Java not running on my Linux machine after installation?
If Java isn't running, it might be due to incorrect environment variable settings or a missing Java runtime. Ensure that JAVA_HOME and PATH are correctly set, and verify Java’s version by typing java -version in the terminal. If the issue persists, reinstalling Java or checking for any missing dependencies may resolve the issue.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.