- Replies:
- 0
- Words:
- 5939

java -version
If Java is already installed, you’ll see output showing the version. Something like:
openjdk version "17.0.8"
If you see a “command not found” error, it means Java isn’t installed yet. Let’s fix that next.
sudo apt update && sudo apt install openjdk-17-jdk
sudo dnf install java-17-openjdk-devel
sudo yum install java-17-openjdk-devel
This will install both the Java runtime (JRE) and the development tools (JDK) needed to compile Java code.
.tar.gz
or .rpm
file for Linuxjava -version
Expected output:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment ...
Also check if the compiler is available:
javac -version
You should see something like:
javac 17.0.8
If both commands show a version number, you're all set!
JAVA_HOME
variable helps other programs (like Maven, Gradle, or IDEs) know where Java is installed.
readlink -f $(which java)
You might see something like:
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
Your JAVA_HOME
is one level up from bin
, so in this case:
/usr/lib/jvm/java-17-openjdk-amd64
nano ~/.bashrc
Or for Zsh users:
nano ~/.zshrc
Add this line at the end of the file:
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export PATH="$JAVA_HOME/bin:$PATH"
Save the file, then apply the changes:
source ~/.bashrc
Now check:
echo $JAVA_HOME
It should print the path you just set.
HelloWorld.java
:
nano HelloWorld.java
Paste this in:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java on Linux!");
}
}
Save and close the file, then compile it:
javac HelloWorld.java
Now run it:
java HelloWorld
You should see:
Hello, Java on Linux!
Nice work!
image quote pre code