How to Write, Compile and Run a Java Program
Every Java journey starts with a first program. In this tutorial you will write a Java program, compile it into bytecode with javac, and run it with java. Along the way you will learn what every word of the classic "Hello" program means, how to pass arguments and read keyboard input, and how to fix the errors beginners meet most often.
Before you start, make sure a JDK is installed. If javac -version does not work in your terminal, follow the Java environment setup guide first.
1. The Three Steps
1. Write HelloJava.java (source code, in any text editor or IDE)
2. Compile javac HelloJava.java -> HelloJava.class (bytecode)
3. Run java HelloJava -> the JVM executes the bytecode
2. Write the Program
Create a folder such as C:\java-practice (or ~/java-practice), open a text editor, type the code below and save it as HelloJava.java. The file name must match the public class name exactly, including capital letters.
public class HelloJava {
public static void main(String[] args) {
System.out.println("Welcome to the world of Java programming!");
}
}
3. Understanding Each Part
| Code | Meaning |
|---|---|
public class HelloJava | Declares a class named HelloJava. All Java code lives inside classes (in a Java 25 compact source file, the compiler declares the class for you). public means it is visible everywhere. |
{ ... } | Braces mark the start and end of the class body and the method body. |
public static void main(String[] args) | The entry point that the java launcher calls to start the program. This classic form works in every Java version; Java 25 also accepts simpler forms (see Java 25 updates). |
public | The launcher must be able to call the method from outside the class. Since Java 25, any non-private main works, but public is still the convention. |
static | The method can run without creating an object of the class. |
void | The method does not return a value. |
String[] args | An array that receives command-line arguments. |
System.out.println(...) | Prints text followed by a new line. System is a class, out is its standard output stream, and println is a method. |
; | Ends a statement. |
4. Compile with javac
Open a terminal (Command Prompt on Windows), go to the folder that contains your file and run javac:
cd C:\java-practice
javac HelloJava.java
If there are no errors, the command prints nothing and creates HelloJava.class next to your source file. That file contains bytecode, which the JVM can run on any operating system.
5. Run with java
Now start the JVM and give it the class name (without .class):
java HelloJava
Welcome to the world of Java programming!
javac and run java HelloJava.java. Java compiles the file in memory and runs it immediately. Since Java 22, the program can also use other source files in the same folder.6. Passing Command-Line Arguments
Words typed after the class name are passed to main in the args array:
public class Greet {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "] = " + args[i]);
}
String name = args.length > 0 ? args[0] : "friend";
System.out.println("Hello, " + name + "!");
}
}
javac Greet.java
java Greet Asha Mumbai 2026
Number of arguments: 3 args[0] = Asha args[1] = Mumbai args[2] = 2026 Hello, Asha!
Arguments are always strings. Convert them when you need numbers, for example Integer.parseInt(args[2]).
7. Reading Input from the Keyboard
Interactive programs read input with the Scanner class:
import java.util.Scanner;
public class AddNumbers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = in.nextInt();
System.out.print("Enter the second number: ");
int b = in.nextInt();
System.out.println();
System.out.println(a + " + " + b + " = " + (a + b));
}
}
Enter the first number: 25 Enter the second number: 17 25 + 17 = 42
8. Java Version Improvements and Java 25 Updates
Writing and running a first program has become simpler over the last few releases:
| Feature | Java version | Status in Java 25 | What it changes |
|---|---|---|---|
| Launch single-file source programs | Java 11 | Standard | java HelloJava.java compiles and runs a file in one step. |
| Launch multi-file source programs | Java 22 | Standard | The source launcher also compiles other .java files the program uses. |
Compact source files and instance main methods | Java 25 | Standard | No class declaration, no public static and no String[] args needed for a small program. |
java.lang.IO | Java 25 | Standard | IO.println() and IO.readln() for simple console input and output. |
The Same Program in Java 25
With Java 25, the HelloJava program from step 2 can be written as a compact source file. The compiler declares the class implicitly, main needs no modifiers or parameters, and IO.println() replaces System.out.println():
void main() {
IO.println("Welcome to the world of Java programming!");
}
java HelloJava.java
# or compile first, as before
javac HelloJava.java
java HelloJava
Welcome to the world of Java programming!
Compact source files are meant for small programs and learning. As a program grows, you can wrap the same code in a class declaration; if it uses library classes such as List, also add the usual import statements, because only compact source files import them automatically. On JDK 21 or older, this file does not compile; use the classic version from step 2 instead.
9. Using an IDE
For larger programs, an IDE does the compiling and running for you. In IntelliJ IDEA, Eclipse or VS Code:
- Create a new Java project and select your JDK.
- Add a class, for example
HelloJava, and type themainmethod (most IDEs generate it from a template; in IntelliJ IDEA, typepsvmormainand press Tab). - Click the green Run button next to
main. The output appears in the console panel.
IDEs also underline errors as you type and suggest fixes, which speeds up learning.
10. Common Errors and How to Fix Them
Compiler errors show the file, line number and a short message. For example, forgetting a semicolon produces:
Broken.java:3: error: ';' expected
System.out.println("Missing semicolon")
^
1 error
| Error | Cause | Fix |
|---|---|---|
';' expected | Missing semicolon | Add ; at the end of the statement shown. |
cannot find symbol | A misspelled name, wrong capital letter or missing import | Check spelling and case (String, not string). |
class HelloJava is public, should be declared in a file named HelloJava.java | File and class names differ | Rename the file or the class so they match exactly. |
'javac' is not recognized | JDK not on PATH | Fix the environment variables and open a new terminal. |
Could not find or load main class HelloJava | Wrong folder, wrong name, or .class added | Run from the folder with HelloJava.class, using just java HelloJava. |
Main method not found in class | Missing or misspelled main method | Use public static void main(String[] args) (or void main() on Java 25). |
implicitly declared classes are not supported in -source 21 | A Java 25 compact source file compiled with an older JDK or --release | Use JDK 25 or later, or add a class declaration and a classic main method. |
reached end of file while parsing | Missing closing brace | Check that every { has a matching }. |
11. Rules for a Valid Java Program
- Java is case-sensitive:
Main,mainandMAINare different. - A file can contain only one
publictop-level class, named like the file. - Every statement ends with a semicolon.
- Code blocks are enclosed in braces.
- Class names use PascalCase; method and variable names use camelCase.
- Program execution starts at
main.
12. Summary
- Write the code in a
.javafile named after its public class. - Compile with
javac FileName.javato create a.classbytecode file. - Run with
java ClassName, orjava FileName.javafor single files. mainreceives command-line arguments inargs;Scannerreads keyboard input.- Read compiler messages carefully: they give the exact line and problem.
Related Tutorials
13. Official Java Documentation References
- The java Command (Java SE 25)
- The javac Command (Java SE 25)
- Java Language Specification (Java SE 25) §12.1.4: Invoke a main Method
- JLS §7.3: Compilation Units
- java.lang.IO (Java SE 25 API)
- java.util.Scanner (Java SE 25 API)
- JEP 512: Compact Source Files and Instance Main Methods
- JEP 458: Launch Multi-File Source-Code Programs
- JEP 330: Launch Single-File Source-Code Programs
- dev.java: Getting Started with Java