Java is still one of the best and most popular programming languages out there; it powers mobile apps and enterprise software. This tutorial will help you install and verify Java 17, compile and run Java programs, work with JAR files, and configure your environment variables on a Linux machine. Now, let’s dive into it.
Installing Java 17 on Linux
Easy way to install Java 17 is with your system’s package manager. For most distributions, OpenJDK is recommended.
For Ubuntu/Debian:
Update Your System
sudo apt update && sudo apt upgrade -y
Install OpenJDK 17
sudo apt install openjdk-17-jdk -y
For CentOS/RHEL/Fedora:
sudo dnf update -y
Enable EPEL Repository (if needed)
sudo dnf install epel-release -y sudo dnf install java-17-openjdk-devel
Verifying Java Installation
After installation, verify your Java version by running:
java -version openjdk version "17.0.17" LTS OpenJDK Runtime Environment (Red_Hat-17.0.17.0.10-1.0.1) (build 17.0.17+10-LTS) OpenJDK 64-Bit Server VM (Red_Hat-17.0.17.0.10-1.0.1) (build 17.0.17+10-LTS, mixed mode, sharing)
You should see an output indicating Java 17 is installed.
Creating a Java Program in Linux
Use any text editor to create a new Java source file. For example, using nano:
nano HelloWorld.java
Insert the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save and exit the editor (for nano: press Ctrl+O, then Enter, then Ctrl+X).
Compiling and Running a Java Program
To compile your Java program, run:
javac HelloWorld.java ls HelloWorld.class HelloWorld.java
This generates a HelloWorld.class file. To execute the program:
java HelloWorld
You should see:
Hello, World!
Finding the Java Installation Path
To determine where Java is installed, run:
which java /usr/bin/java
This shows the symbolic link. For the actual installation directory, use:
readlink -f $(which java) /usr/lib/jvm/java-17-openjdk-17.0.17.0.10-1.0.1.el9.x86_64/bin/java
Or, to list all installed Java versions with their paths:
update-alternatives --config java There is 1 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.17.0.10-1.0.1.el9.x86_64/bin/java) Enter to keep the current selection[+], or type selection number: 1
Creating a JAR File Using a Java Program
First, ensure your program is compiled (.class files exist). Then, use the jar command:
jar cvf HelloWorld.jar HelloWorld.class added manifest adding: HelloWorld.class(in = 427) (out= 290)(deflated 32%)
- c creates a new JAR file.
- v enables verbose output.
- f specifies the filename.
To create an executable JAR, include a manifest specifying the main class:
echo "Main-Class: HelloWorld" > manifest.txt jar cfm HelloWorld.jar manifest.txt HelloWorld.class ls HelloWorld.class HelloWorld.jar HelloWorld.java manifest.txt
Executing a JAR File in Linux
To run your JAR file:
java -jar HelloWorld.jar
You should again see:
Hello, World!
Setting JAVA_HOME in Linux
Setting JAVA_HOME ensures that development tools and scripts can locate your Java installation.
-
Find your Java installation directory:
sudo update-alternatives --config java
The path will look something like /usr/lib/jvm/java-17-openjdk-17
-
Open your shell profile (.bashrc, .bash_profile, or .profile) in a text editor:
nano ~/.bashrc
-
Add the following line at the end:
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-17.0.17.0.10-1.0.1.el9.x86_64" export PATH="$JAVA_HOME/bin:$PATH"
-
Apply the changes:
source ~/.bashrc
-
Verify:
echo $JAVA_HOME /usr/lib/jvm/java-17-openjdk-17.0.17.0.10-1.0.1.el9.x86_64
Conclusion
You may now efficiently create, compile, and package Java applications on your Linux machine with Java 17 installed. Having a solid grasp of these fundamental stages guarantees a seamless development experience, regardless of whether you’re working with basic apps or huge corporate solutions. More new setups and in-depth tutorials are coming soon!