00:00

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

Workflow
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.

HelloJava.java
public class HelloJava {
    public static void main(String[] args) {
        System.out.println("Welcome to the world of Java programming!");
    }
}
Saving the Java source file

3. Understanding Each Part

CodeMeaning
public class HelloJavaDeclares 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.
publicThe JVM must be able to call the method from outside the class.
staticThe method can run without creating an object of the class.
voidThe method does not return a value.
String[] argsAn 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:

Terminal
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.

Compiling a Java program with javac

5. Run with java

Now start the JVM and give it the class name (without .class):

Terminal
java HelloJava
Output
Welcome to the world of Java programming!
Output of the Java program in the terminal
Shortcut (Java 11+): for a single-file program you can skip 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:

Greet.java
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 + "!");
    }
}
Terminal
javac Greet.java
java Greet Asha Mumbai 2026
Output
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:

AddNumbers.java
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));
    }
}
Output (user input shown after the prompts)
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:

  1. Create a new Java project and select your JDK.
  2. Add a class, for example HelloJava, and type the main method (type psvm or main and press Tab to generate it).
  3. 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:

javac output
Broken.java:3: error: ';' expected
        System.out.println("Missing semicolon")
                                               ^
1 error
ErrorCauseFix
';' expectedMissing semicolonAdd ; at the end of the statement shown.
cannot find symbolA misspelled name, wrong capital letter or missing importCheck spelling and case (System, not system).
class HelloJava is public, should be declared in a file named HelloJava.javaFile and class names differRename the file or the class so they match exactly.
'javac' is not recognizedJDK not on PATHFix the environment variables and open a new terminal.
Could not find or load main class HelloJavaWrong folder, wrong name, or .class addedRun from the folder with HelloJava.class, using just java HelloJava.
Main method not found in classMisspelled main signatureUse public static void main(String[] args).
reached end of file while parsingMissing closing braceCheck that every { has a matching }.

10. Rules for a Valid Java Program

  • Java is case-sensitive: Main, main and MAIN are different.
  • A file can contain only one public top-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 .java file named after its public class.
  • Compile with javac FileName.java to create a .class bytecode file.
  • Run with java ClassName, or java FileName.java for single files.
  • main receives command-line arguments in args; Scanner reads keyboard input.
  • Read compiler messages carefully: they give the exact line and problem.

Related Tutorials

Reference: dev.java: Getting Started with Java