Module 1: Start Coding in Java
Compile, Run, and Debug a Local Java Program
Create `Main.java`, compile it into bytecode, run the class, understand why edits require recompilation, and repair realistic source, file-name, and terminal mistakes.
Author
Java Learner
Java version
Java 25 LTS
Learning goals
- Create a correctly named `Main.java` source file
- Compile source with `javac Main.java`
- Run the compiled class with `java Main`
- Explain why an edited source file must be recompiled
- Use the first compiler message to investigate a problem
- Repair punctuation and public class/file-name mismatches
Before you start
- A Java 25 JDK is installed
- `java -version` and `javac -version` both report major version 25
- A `java-course` folder is open in an editor and terminal
Create the source file in the current workspace. Add a file named exactly Main.java. Capitalization matters. A file named main.java is not the same name on every operating system and does not match the public class used in this lesson.
Match the public class to the file name. A file named Main.java should contain public class Main. If the public class is named App, Java expects the source file to be named App.java.
Save before compiling. The terminal compiler reads the contents saved on disk. If your editor contains unsaved changes, javac still sees the older saved version.
Compile from the folder that contains the source file. Run javac Main.java. If the terminal says the file was not found, check the current folder and the exact file name before changing the Java code.
A successful compile is usually quiet. When compilation succeeds, javac creates Main.class. That file contains bytecode for the JVM. No news is usually good news: the compiler often prints nothing when it succeeds.
Run the class name, not a file name. Use java Main. Do not add .java or .class. The launcher receives the class name and starts its main method.
Editing source does not update the class automatically. If you change Main.java and immediately run java Main without recompiling, the JVM can still run the older Main.class. Save, compile, and run after every source change.
Separate compile-time errors from program output. javac Main.java checks whether the source follows Java syntax and type rules. java Main runs the compiled class. Compiler messages are not output produced by your program.
Start with the first compiler message. Later messages may be side effects of the first missing character. Inspect the reported line and the line immediately above it. A missing quote or semicolon often causes the compiler to point near the following token.
Fix one cause and compile again. Do not make several unrelated edits while diagnosing an error. One correction followed by another compilation gives clearer evidence.
Use the exact command sequence until it becomes familiar. Save Main.java, run javac Main.java, then run java Main. Later, build tools and IDEs can automate these steps, but understanding the manual cycle makes their behavior easier to debug.
Runnable examples
Create a local program
public class Main {
public static void main(String[] args) {
System.out.println("Source file: Main.java");
System.out.println("Compiled class: Main.class");
System.out.println("Launch command: java Main");
}
}Expected output
Source file: Main.java Compiled class: Main.class Launch command: java Main
Compile first, then run
Run in a terminaljavac Main.java
java MainExpected output
Source file: Main.java Compiled class: Main.class Launch command: java Main
Repeat the cycle after an edit
Run in a terminaljavac Main.java
java MainExpected output
The output comes from the newly compiled source rather than an older `Main.class` file.
Practice the lesson
Predict first, make a change, repair a real problem, then write a small program independently.
Predict the result
You compile a program that prints `Version 1`. Then you edit the source so it prints `Version 2`, save it, and run `java Main` without running `javac` again. What will probably appear, and why?
Show answer
`Version 1` will probably appear because `java Main` runs the existing `Main.class`, which still contains the previously compiled bytecode.
Saving changes updates `Main.java`, not `Main.class`. Recompile the source before running if you want the class file to contain the new instructions.
Change the program
Add a fourth line to the working program, then complete the full save, compile, and run cycle. Verify that the new line does not appear until the source has been recompiled.
public class Main {
public static void main(String[] args) {
System.out.println("Step 1: save the source");
System.out.println("Step 2: compile the source");
System.out.println("Step 3: run the class");
}
}Success criteria
- • The source file is saved as `Main.java`.
- • `javac Main.java` completes without errors.
- • `java Main` prints all four lines in order.
- • The added line appears only after the updated source is compiled.
- • No `.java` or `.class` extension is passed to the `java` command.
Match the public class and source file
This code is saved in a file named `Main.java`. Read the compiler message, then fix the mismatch without changing the intended output.
public class App {
public static void main(String[] args) {
System.out.println("The class and file names now match.");
}
}What happens: The compiler reports that the public class `App` should be declared in a file named `App.java`.
Show the correction
public class Main {
public static void main(String[] args) {
System.out.println("The class and file names now match.");
}
}Independent challenge
Repair, compile, and run the damaged `Main.java` program below. Work from the first compiler message and make one correction at a time. ```java public class EnvironmentCheck { public static void main(String[] args) { System.out.println("Java version checked") System.out.println("Compiler checked); } System.out.println("Local workflow ready"); } ```
- • Assume the source is saved as `Main.java`.
- • Make the public class name match the file.
- • Close every string literal.
- • Add every missing semicolon.
- • Keep all executable statements inside `main`.
- • Compile with `javac Main.java` and run with `java Main`.
- • Confirm that the final output has exactly three lines.
Show one complete solution
public class Main {
public static void main(String[] args) {
System.out.println("Java version checked");
System.out.println("Compiler checked");
System.out.println("Local workflow ready");
}
}Expected output
Java version checked Compiler checked Local workflow ready
Common mistakes
Running `java Main.class`
Pass the class name only: `java Main`.
Running `java Main.java` after learning the compile-and-run workflow
For this lesson, compile explicitly with `javac Main.java`, then run the class with `java Main`.
Changing the source and seeing the old output
Save the file and run `javac Main.java` again before `java Main`.
Compiling from the wrong directory
Move into the folder that contains `Main.java`, then rerun the compile command.
Using `public class App` inside `Main.java`
Rename the class to `Main` or rename the file to `App.java`. The public class and file names must match.
Trying to fix every compiler message at once
Begin with the first message and one likely cause. Recompile to see which later messages disappear.
Summary
- Save the source as `Main.java` and match it with `public class Main`.
- `javac Main.java` compiles source into `Main.class`.
- `java Main` runs the compiled class without a file extension.
- Save and recompile after every source change.
- A missing source file usually means the terminal is in the wrong folder or the name is incorrect.
- Read the first compiler message, inspect nearby lines, fix one cause, and compile again.
Keep the reference close
Next step
You now have a complete local workflow. In Module 2, replace fixed text with variables, primitive values, user input, and type conversions.
Sources used