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. 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. The JVM looks for exactly this method to start the program. |
public | The JVM must be able to call the method from outside the class. |
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.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. 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 (typepsvmormainand press Tab to generate it). - 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.
9. 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 (System, not system). |
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 | Misspelled main signature | Use public static void main(String[] args). |
reached end of file while parsing | Missing closing brace | Check that every { has a matching }. |
10. 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.
11. 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
Reference: dev.java: Getting Started with Java
Frequently Asked Questions (FAQ)
Q1. How do I write my first Java program?
Create a file named HelloJava.java containing a public class HelloJava with a public static void main(String[] args) method that calls System.out.println("..."). Then compile with javac and run with java.
Q2. What is the difference between javac and java?
javac is the compiler: it turns .java source files into .class bytecode. java is the launcher: it starts the JVM and runs a compiled class (or, since Java 11, a single source file).
Q3. Why must the file name match the class name?
Java requires a public top-level class to be declared in a file with the same name. This lets the compiler and tools find classes quickly by name.
Q4. Can I run a Java file without compiling it?
Yes, since Java 11: java HelloJava.java compiles the file in memory and runs it. This works for programs contained in one source file (newer versions also support multi-file launches).
Q5. Why is main declared as public static void?
public lets the JVM call it, static lets it run without creating an object, and void means it returns nothing. The JVM looks for exactly this signature.
Q6. Can I write String args[] instead of String[] args?
Yes, both are valid array declarations. String... args also works. String[] args is the conventional style.
Q7. What is System.out.println?
System is a class in java.lang, out is its static PrintStream field connected to standard output, and println() prints a value followed by a line break. print() prints without the line break.
Q8. What does "Could not find or load main class" mean?
The JVM could not find the class you asked it to run. Check that you are in the right folder, that the name matches exactly (case-sensitive), that you did not type .class, and that the code compiled successfully.
Q9. Where is the .class file created?
By default, in the same folder as the source file. Use javac -d out HelloJava.java to write class files into another folder, then run with java -cp out HelloJava.
Q10. What are command-line arguments?
Values typed after the class name when running a program, such as java Greet Asha. They arrive in main as the String[] args array.
Q11. How do I take input from the user in Java?
Create a Scanner on System.in and call methods such as nextInt(), nextDouble() or nextLine(). See Scanner class in Java.
Q12. Is Java case-sensitive?
Yes. Keywords, class names, method names and variables must use the exact capitalisation, so system.out.println fails while System.out.println works.
Q13. Can a Java program have more than one class?
Yes. A program usually has many classes, typically one per file. A single file can contain several classes, but only one of them can be public.
Q14. Which editor should I use to write Java?
Any plain-text editor works for small programs. For real projects, IntelliJ IDEA Community, Eclipse or VS Code with the Java extensions give auto-completion, error highlighting and debugging.
Q15. What is a compile-time error?
An error found by javac before the program runs, such as a missing semicolon or a type mismatch. The compiler shows the file, line and description, and no .class file is produced until it is fixed.
Q16. What is a runtime error?
An error that happens while the program runs, such as dividing by zero or using a null reference. Java reports it as an exception with a stack trace showing where it occurred.
Q17. Can I compile several Java files at once?
Yes: javac *.java, or list the files. For real projects, build tools such as Maven and Gradle compile everything for you.
Q18. What is the simplest possible Java program in newer versions?
Java 25 finalized compact source files and instance main methods, so a single file can contain just void main() { IO.println("Hello"); }. The classic class-based version still works everywhere and is what most code uses.
Q19. Do I need to recompile after changing the code?
Yes. The JVM runs the .class file, so changes to the .java file take effect only after you compile again (IDEs do this automatically).
Q20. What is the difference between print and println?
print() writes text and leaves the cursor on the same line; println() writes text and moves to the next line. printf() writes formatted text, for example System.out.printf("%.2f%n", price);.