To write a hello world program in Java, open Notepad, type a class with a main method, and use System.out.println("Hello, World!");. Save the file as HelloWorld.java, compile with javac HelloWorld.java in Command Prompt, then run with java HelloWorld. Your output appears instantly in the terminal.
- Key Takeaway 1: You need the JDK (Java Development Kit) installed before you can compile or run any Java file.
- Key Takeaway 2: The filename must exactly match the class name, including capital letters, or javac will refuse to compile.
- Key Takeaway 3:
public static void main(String[] args)is the entry point every Java program needs to execute. - Key Takeaway 4: If
javacis not recognised by your terminal, your PATH variable is not set correctly. It is fixable in two minutes. - Key Takeaway 5: Java consistently ranks in the top 4 languages on the TIOBE Index, making this a skill worth building properly from day one.
Setting Up Java for Your Hello World Program
Before you write a single line of a hello world program in Java, your machine needs the JDK (Java Development Kit). The JDK bundles the compiler (javac), the runtime (java), and the standard libraries. Without it, nothing works. The JRE (Java Runtime Environment) alone lets you run Java programs but not compile them, so do not install just the JRE.
Download the latest LTS version from Oracle’s official Java downloads page. As of 2025, JDK 21 is the current LTS release. If you are on a college lab machine in India running Windows, there is a good chance JDK 8 or JDK 11 is already installed. Universities affiliated with Anna University, Mumbai University, and VTU commonly ship lab systems with JDK 8 pre-loaded for first-year BCA and BSc CS students.
Installing on Windows (Notepad workflow)
- Download the JDK installer (.exe) from Oracle and run it.
- Note the installation path, usually
C:\Program Files\Java\jdk-21. - Open System Properties, go to Environment Variables, and find the Path variable under System variables.
- Click Edit, add a new entry:
C:\Program Files\Java\jdk-21\bin. - Click OK, close everything, then open a fresh Command Prompt and type
javac -version. You should see the version number printed.
That PATH step is where most beginners get stuck. The bin folder holds javac.exe and java.exe. If Windows cannot find them, you will see the error “javac is not recognised as an internal or external command”. Setting PATH fixes it every time.
Checking your setup
Open Command Prompt and run two quick checks:
javac -version— should print something likejavac 21.0.2java -version— should print the runtime version
If both return version numbers, you are ready. If you are curious how this compares to setting up other languages, the process is similar to what you would do when writing and running a Python program or compiling and running a C program, though Java adds the explicit compile step every time.
Writing the Hello World Program in Java Using Notepad
Open Notepad. Do not use Word or any rich text editor. They add invisible formatting characters that break compilation. Notepad is plain text, which is exactly what javac expects.
Type the following code exactly as shown:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Now go to File > Save As. In the filename field, type HelloWorld.java. Change the “Save as type” dropdown to All Files. If you leave it as “Text Documents”, Notepad will silently save it as HelloWorld.java.txt and javac will not find it. Save it somewhere easy to reach, like C:\JavaProjects\.
Choosing the right tools matters as your projects grow. If you want to understand which editors and IDEs professional developers use day to day, the guide on software development tools to learn first is a solid next read after you have your hello world program in Java working.
Why the filename matters so much
Java is case-sensitive about class names and filenames. If your class is named HelloWorld, the file must be HelloWorld.java, not helloworld.java or Helloworld.java. The compiler enforces this strictly. This trips up a large number of first-time Java programmers in Indian college labs where auto-correct or default lowercase naming is common.
Compiling and Running Your Hello World Program in Java
Open Command Prompt. Navigate to the folder where you saved your file. If you saved it in C:\JavaProjects\, type:
cd C:\JavaProjectsand press Enter.javac HelloWorld.javaand press Enter.- If there are no errors, type
java HelloWorldand press Enter.
You will see Hello, World! printed in the terminal. That is it. You have written, compiled, and run your first Java program.
What happens during compilation
When you run javac HelloWorld.java, the compiler translates your source code into bytecode and creates a file called HelloWorld.class. That .class file is not machine code specific to Windows or Linux. It is platform-neutral bytecode that the JVM (Java Virtual Machine) can run on any operating system. This is why Java’s tagline was “write once, run anywhere.”
When you then type java HelloWorld, the JVM reads HelloWorld.class and executes it. Notice you do not type the .class extension here. Typing java HelloWorld.class is a common mistake that produces an error.
Common compile-time errors to watch for
| Error message | Likely cause | Fix |
|---|---|---|
| javac is not recognised | JDK bin folder not in PATH | Add bin path to System Environment Variables |
| class HelloWorld is public, should be declared in a file named HelloWorld.java | Filename does not match class name | Rename the file to match exactly |
| reached end of file while parsing | Missing closing brace | Check every opening { has a matching } |
| cannot find symbol: System.out.println | Typo in method name or wrong capitalisation | Java is case-sensitive. Use exact spelling |
| error: class HelloWorld is not found | Running java from wrong directory | cd into the folder containing HelloWorld.class |
If you start running into runtime issues after compilation succeeds, the page on Java exceptions and common runtime errors covers what happens when things go wrong at execution time.
Line-by-Line Explanation of the Hello World Program in Java
Understanding what each line does turns this from a copy-paste exercise into actual knowledge. Here is the complete breakdown.
public class HelloWorld
class defines a class, which is the basic building block of any Java program. Every piece of Java code lives inside a class. public means this class is accessible from anywhere. HelloWorld is the name you gave the class, and it must match the filename.
public static void main(String[] args)
This is the entry point of every Java application. The JVM looks for this exact method signature to know where to start execution. Break it down:
- public: the JVM needs to call this from outside the class, so it must be public.
- static: the JVM calls this without creating an object first, so it must be static.
- void: this method does not return any value.
- main: the exact name the JVM expects. Spell it differently and the program will not run.
- String[] args: an array of strings that can receive command-line arguments. You are not using them here, but the parameter must be present.
System.out.println(“Hello, World!”);
System is a built-in Java class. out is a static field inside System that refers to the standard output stream (your terminal). println is a method that prints text and moves to a new line. The text inside the quotes is called a string literal. The semicolon at the end is mandatory. Java uses it to mark the end of a statement.
If you use System.out.print instead of println, the output appears without a line break. Useful to know when you start printing multiple values.
The curly braces
The outer { } wraps the class body. The inner { } wraps the method body. Every opening brace needs a matching closing brace. Mismatched braces cause the “reached end of file while parsing” error shown in the table above.
Java’s syntax here is quite similar to C, which makes sense given Java’s design history. If you have already worked through a C program from scratch, the structure will feel familiar. Students studying BSc Computer Science subjects and scope often learn C and Java in the same semester for exactly this reason.
Why Learning the Hello World Program in Java Still Matters in 2025
According to the Stack Overflow Developer Survey 2024, Java is used by approximately 30% of professional developers worldwide, placing it consistently among the top five most-used languages globally. The TIOBE Index (April 2024) ranks Java fourth globally among all programming languages. Additionally, according to the GitHub Octoverse 2023 report, Java ranked third in total repository contributions worldwide, with particularly strong activity from developers in India, Indonesia, and Brazil, confirming its dominance in emerging tech markets.
In India specifically, Java remains one of the most demanded skills in job postings on Naukri and LinkedIn for backend developer and Android developer roles. Freshers with Java proficiency command starting salaries between 3.5 and 6 LPA at mid-size IT firms, according to Naukri’s 2024 salary insights report.
The choice between operating systems can affect your development workflow too. If you are deciding whether to code on Linux or Windows long-term, the comparison on Linux vs Windows for developers lays out the practical differences clearly.
If you want to apply your Java knowledge to real-world cybersecurity work, explore the CEH v13 AI online course at 3.0 University. Knowing how code runs is the first step to knowing how it breaks.
Frequently Asked Questions
How do I write a hello world program in Java?
Open Notepad and create a public class named HelloWorld. Inside it, add the public static void main(String[] args) method, and within that method write System.out.println("Hello, World!");. Save the file as HelloWorld.java, compile with javac HelloWorld.java, and run with java HelloWorld. That is the complete hello world program in Java.
How do I run Java from Notepad?
Notepad is only for writing the code. You compile and run from Command Prompt, not from Notepad itself. Open Command Prompt, navigate to the folder containing your .java file using cd, run javac HelloWorld.java to compile, then java HelloWorld to execute. The output appears in the terminal window.
What does public static void main mean?
public makes the method accessible to the JVM from outside the class. static means the JVM can call it without creating an object. void means it returns nothing. main is the specific name the JVM looks for as the program’s starting point. Miss any of these keywords and your hello world program in Java will not execute.
Why does javac not work?
The most common reason is that the JDK’s bin folder is not added to your system’s PATH variable. Open Environment Variables in Windows, find the Path entry under System Variables, and add the full path to your JDK’s bin folder, for example C:\Program Files\Java\jdk-21\bin. Open a fresh Command Prompt after saving and try again.
What should I learn after hello world?
After your hello world program in Java, move on to variables and data types, conditional statements, loops, and then methods. Once you are comfortable with those, tackle object-oriented concepts like classes, objects, and inheritance. Strong Java fundamentals open doors to Android development, backend engineering, and secure coding. Browse 3.0 University’s online certification courses to find a structured path forward.
You have now written, compiled, and understood every line of the hello world program in Java. That is a real milestone. Every Java developer, from a fresher at a Pune startup to a senior engineer at an MNC, started exactly here.
- This week: Modify the program to print your name and your college. Try changing
printlntoprintand observe the difference. Small experiments build intuition faster than reading alone. - Next: Work through variables, data types, and basic loops in Java. Then explore Java exceptions and runtime errors so you know how to read and fix error messages like a professional.
- Build toward something bigger: Programming fundamentals are the foundation for ethical hacking, AI development, and secure software engineering. Explore the CEH v13 AI online course at 3.0 University, or browse the full catalogue of certification courses in Cybersecurity, Blockchain, and AI.
Last updated: June 2025. Reviewed by the 3University editorial team.