To install Java and run Java code in Linux (Ubuntu or other distributions), follow these steps:Documentation Index
Fetch the complete documentation index at: https://programming-for-career.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Step 1: Install Java
Option 1: Install OpenJDK (Recommended)
Check Available Java Versions
Install OpenJDK 17 (Recommended)
openjdk-17-jdk with openjdk-21-jdk.
Verify Java Installation
Option 2: Install Oracle JDK (If Required)
Oracle JDK requires manual installation. Use this if OpenJDK is not suitable for your needs.- Download Oracle JDK from the official Oracle website.
- Extract it and set up environment variables manually.
Step 2: Run Java Code
Method 1: Compile and Run Java Code (Without an IDE)
- Create a Java file:
- Add the following Java code:
- Save the file (
Ctrl + X, thenY, thenEnter). - Compile the Java file:
This generates a
HelloWorld.classfile. - Run the Java program:
Expected output:
Method 2: Run Java Code Using jshell (Interactive Mode)
If you installed Java 9+, you can use jshell for quick testing:
jshell using:
Step 3: Set JAVA_HOME (If Needed)
To set theJAVA_HOME environment variable: