Your first Java program
20 min · Beginner
Showing steps for Windows. They are marked “On Windows” below.
A small Java program is a class with a main method and a few statements. Write one, save it as Hello.java and run it, in your IDE or in the terminal.
The parts of a Java program
Look at the example program below. It has a few important parts.
A Java statement such as System.out.println("Hello"); ends with a semicolon ;, like a full stop ends a sentence. Other parts, such as the class and method lines, follow different rules: they use curly brackets { } instead.
public class Hello– a class is a block that holds your code. This class is namedHello.public static void main(String[] args)– the main method. In this course, we will initially use the traditional Java program structure, where execution begins here. Newer Java versions also support simpler forms, which we can explore later.System.out.println("...");– prints the text inside the quotes and then moves to a new line.{ }– curly brackets show where the class and the main method start and end.
Save the file with the right name
In your IDE, create a class named Hello. The IDE saves it as Hello.java for you. Without an IDE, type the program in any text editor, such as Notepad or TextEdit, and save it as Hello.java in a practice folder (next section).
A public top-level class named Hello must be declared in a file named Hello.java. Top-level means the class is not inside another class.
Java cares about capital letters. Hello.java and hello.java are different names.
Hello.java.txt. On a Mac, TextEdit must be in plain text mode (Format → Make Plain Text).Open a terminal in your practice folder
Skip this if you only use your IDE. To use the terminal, keep your files in one simple folder and open the terminal there. Do this once; later just open the folder again.
On Windows
Use a folder like C:\java-practice. Your Desktop may be inside OneDrive, so cd Desktop often fails; a simple folder avoids that.
- Open Command Prompt. Type
mkdir C:\java-practiceand press Enter (only the first time). - Save
Hello.javaintoC:\java-practice. - Type
cd C:\java-practiceand press Enter. - Type
dirand press Enter. You should seeHello.javain the list.
cmd and press Enter. Command Prompt opens in that folder.Run it: IDE or terminal
In your IDE (from the previous lesson), click Run. The IDE compiles and runs the program in one step, and shows the output at the bottom.
In the terminal, type these two commands in your practice folder. In this workflow, javac Hello.java compiles the file and java Hello launches the compiled class.
javac Hello.java– the compiler checks your code and makesHello.class(bytecode).java Hello– the JVM runs your program. Type the class name only: neverjava Hello.class.
java Hello.java. This course uses javac and then java Hello, so you see both steps.Common beginner mistakes
Everyone makes these mistakes at first. The compiler shows the line number of the error. Read it, fix that line and compile again.
If something else goes wrong, use the Common errors → fixes list in the Summary tab. Check from the top: first the JDK, then the folder and file name, then the code.
- Missing semicolon – a statement like
System.out.println("Hi");needs;at the end. If not, you see';' expected. - Wrong file name – a
public class Hellomust be saved inHello.java. - Wrong capital letters – write
System, notsystem. Java sees them as different words. - Missing quotes or brackets – text needs double quotes, and every
{needs a}.
java -version works, javac -version works, and your program prints Hello, World!. If all three are true, your Java setup is complete.Example
public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); System.out.println("I am learning Java."); }}Hello, World! I am learning Java.
What this code does
public class Hellomakes a public class namedHello, so the file isHello.java.- The program starts inside
main. - Each
System.out.printlnprints one line, from top to bottom.
What's next
You finished the free preview of Getting Started
Java Programming has 6 sections, 70 modules and 528 lessons. A free account opens every lesson and saves your progress.
Next step
Check your understanding
6 quick questions