Skip to content

Beginner · Getting started with Java · topic 2 of 60 · 30 min

Installing the JDK and your first program

Explanation

Install a current LTS JDK, check it with java -version, then write Hello.java: a public class named Hello with a main method that prints a line. Compile with javac Hello.java and run with java Hello, or run a single file directly with java Hello.java. Compile errors stop the build; run-time errors (exceptions) happen while the program runs.

Step 1: install a JDK

  1. 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.
  2. Run the installer. On Windows, tick the options that set JAVA_HOME and add Java to PATH if offered.
  3. 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

java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Namaste from Java!");
        System.out.println("2 + 3 = " + (2 + 3));
    }
}
Save as Hello.java. The file name must match the public class name, including capital letters.
bash
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 directly
output
Namaste from Java!
2 + 3 = 5
Output.

Every word of Hello World

PartMeaning
public class HelloDefines 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

output
Hello.java:3: error: ';' expected
        System.out.println("Hi")
                                ^
1 error
A compile error: the program is not built at all. Read the file name, line number and the caret ^.
output
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Hello.main(Hello.java:4)
A run-time error (exception): the program compiled but crashed while running, at line 4.
Careful: The three most common first-day mistakes: (1) the file is named hello.java but the class is Hello (Java is case-sensitive, and the public class must match the file name); (2) running java Hello.class instead of java Hello; (3) writing string with a small s or System with a small s.
Think of it like this: javac is like a strict editor at a newspaper: if one comma is missing, the whole article is sent back before printing. The JVM is the printing press: it only runs what passed the editor, but it can still jam (an exception) if something goes wrong while printing.
Tip: Read compiler errors from the top. Fix the FIRST error and compile again; one missing brace can cause twenty follow-on errors.

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

Your first Java program

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.

java
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));
    }
}
Prints the store name, three items, and Total: Rs 375.

Practice (0/3 done)

Task 1 · Question

Your file is Hello.java with public class Hello. Which commands compile and run it?

Task 2 · Question

Which line has the bug? 1: public class Greet { 2: public static void main(String[] args) { 3: system.out.println("Hi"); 4: } 5: }

Task 3 · Practice

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

Question 1

1.What must the file name be for "public class Invoice"?

Question 2

2.What is the difference between print and println?

Question 3

3.System.out.println("Sum: " + 2 + 3) prints…

Question 4

4.An ArithmeticException: / by zero appears. What kind of error is it?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

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.

From Reading errors and debugging

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.

From What Java is and where it is used

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.

From Installing .NET and using the dotnet CLI

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().

From Your first program and Go modules

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

  1. 3 more topics in “Getting started with Java” before its exam: Variables and primitive types, Operators and expressions, Type conversion and reading input.
  2. Module 1 exam: Getting started with Java · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Control flow and methods. 60 topics left in the course before the final exam.

← Previous: What Java is and where it is used