Beginner · Getting started with Java · topic 2 of 60 · 30 min
Installing the JDK and your first program
Explanation
Step 1: install a JDK
- Download a free JDK build of a current LTS version (21 or 25), for example Eclipse Temurin from adoptium.net or Oracle's JDK from oracle.com.
- Run the installer. On Windows, tick the options that set JAVA_HOME and add Java to PATH if offered.
- Open a new terminal and run java -version and javac -version. Both should print the version.
PATH is the list of folders where the terminal looks for programs. JAVA_HOME is a variable that tools like Maven use to find the JDK. If "javac is not recognized" appears, the JDK's bin folder is not on PATH.
Step 2: your first program
public class Hello {
public static void main(String[] args) {
System.out.println("Namaste from Java!");
System.out.println("2 + 3 = " + (2 + 3));
}
}javac Hello.java # compiles → Hello.class
java Hello # runs the class (no .class extension!)
java Hello.java # Java 11+: compile in memory and run a single file directlyNamaste from Java!
2 + 3 = 5Every word of Hello World
| Part | Meaning |
|---|---|
| public class Hello | Defines a class named Hello. All Java code lives inside classes. |
| public static void main(String[] args) | The entry point the JVM calls first. static: no object needed; void: returns nothing; args: command-line arguments. |
| System.out.println(...) | Prints a line to the console. print(...) prints without a new line; printf(...) prints formatted text. |
| { } and ; | Braces group code into blocks; each statement ends with a semicolon. |
Newer Java (25 and later) also lets beginners write a simpler file with just void main() { IO.println("Hi"); } (called a compact source file). You will see the classic form everywhere in real projects, so this course uses it.
Editors and IDEs
An IDE (Integrated Development Environment) highlights errors as you type, completes code and runs programs with one click. Popular free choices: IntelliJ IDEA Community Edition, VS Code with the Extension Pack for Java, and Eclipse. Use one, but also know how to compile from the terminal; interviews and servers use the command line.
Compile errors versus run-time errors
Hello.java:3: error: ';' expected
System.out.println("Hi")
^
1 errorException in thread "main" java.lang.ArithmeticException: / by zero
at Hello.main(Hello.java:4)Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Installing the JDK and your first program · 1/6
In this lesson you will install Java and run your first program.
Prefer a video by a teacher? Find free videos on “install jdk 21 write first java program javac java hello world” on YouTube ↗
In real life: A shop receipt in the console
A first real program for a kirana shop prints a simple receipt: shop name, items and the total. It uses println for lines and string joining with + for values.
public class Receipt {
public static void main(String[] args) {
int rice = 120, dal = 95, oil = 160;
System.out.println("Sharma General Store");
System.out.println("Rice: Rs " + rice);
System.out.println("Dal: Rs " + dal);
System.out.println("Oil: Rs " + oil);
System.out.println("Total: Rs " + (rice + dal + oil));
}
}Practice (0/3 done)
Your file is Hello.java with public class Hello. Which commands compile and run it?
Which line has the bug? 1: public class Greet { 2: public static void main(String[] args) { 3: system.out.println("Hi"); 4: } 5: }
Install a JDK, then write Bio.java that prints three lines: your name, your city, and the result of 365 * 24 (hours in a year). Compile with javac and run with java. Then run it again with java Bio.java.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Variables and primitive typesGetting started with Java · same module
- Operators and expressionsGetting started with Java · same module
- Reading errors and debuggingControl flow and methods
- Installing .NET and using the dotnet CLIC# and .NET · Getting started with C# and .NET
- Compiling with gccC Programming (from zero to systems) · Getting started with C
- Your first program and Go modulesGo Programming (backend and cloud) · Getting started with Go
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What must the file name be for "public class Invoice"?
Answer: Invoice.java
A public class must be in a file with exactly the same name.
What is the difference between print and println?
Answer: println adds a new line after the text
println ends the line; print does not.
System.out.println("Sum: " + 2 + 3) prints…
Answer: Sum: 23
Left to right: "Sum: " + 2 is a String, then + 3 joins "3".
An ArithmeticException: / by zero appears. What kind of error is it?
Answer: Run-time exception
The program compiled; it failed while running.
In a stack trace, which "at" line is the place where the exception happened?Related
Answer: The first one (top)
The top frame is where it was thrown; lines below are the callers.
Which of these is an LTS Java release?Related
Answer: Java 21
17, 21 and 25 are LTS releases; versions in between are short-term.
What do you need to install to BUILD C# programs?Related
Answer: The .NET SDK
The SDK has the compiler and the dotnet tools. The runtime alone can only run built apps.
You write the opening brace of func main on the next line. What happens?Related
Answer: Compile error, because Go inserts a semicolon at the end of the func line
Go automatically inserts semicolons at line ends, so the brace must be on the same line as func main().
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Getting started with Java” before its exam: Variables and primitive types, Operators and expressions, Type conversion and reading input.
- Module 1 exam: Getting started with Java · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Control flow and methods. 60 topics left in the course before the final exam.