Java Compiling
Compile a tiny teaching file and run the class file it produces. In Build.java, keep the public class Build, print ok on the first demo, then print ok and built on the second demo after you change the file and compile again. Run javac Build.java so Build.class appears beside the source. Run java Build — not java Build.java. Leave Mini Project for the next page. This lesson is the compile-and-run habit with a visible .class file, not a new library call and not a course wrap-up.
Compile once, then run the class
Open Build.java. Keep the public class name Build. Delete leftover Recap summary demos, leftover Dice Random rolls, leftover Stamp date prints, and any keyboard Scanner that waits for typing. The program must not wait for input.
Inside Build.main, print one short word:
System.out.println("ok");
From the folder that holds Build.java, run javac Build.java. When braces and semicolons match, javac writes no line of its own. It writes Build.class next to the source. That .class file is what the Java launcher loads.
Then run java Build. Leave the .java ending off the run command. Keep one public class so the name matches Build. The screen shows ok.
public class Build {
public static void main(String[] args) {
System.out.println("ok");
}
}
Walk the run once. javac reads Build.java and writes Build.class. java Build loads Build.class, starts at Build.main, and prints ok. Nothing builds new Random(42). Nothing prints ready 42 from Recap. Nothing freezes a calendar day.
Say the beginner rule while you look at the folder: source ends in .java, bytecode ends in .class, compile with javac, run with java and the class name. Do not invent a Mini Project on this page. Mini Project waits for the next lesson. Do not type java Build.java — that run shape is wrong for this course path.
If you still see ready 42 from a leftover Recap file, switch to Build.java and run java Build after javac Build.java. If javac reports an error, fix the source and compile again before you run. If the run says it cannot find or load the main class Build, confirm you are in the folder that holds Build.class and that you compiled successfully.
Change the source, compile again
The first demo proves one successful compile. The second demo proves that edits need a fresh javac before the screen changes. Keep the same public class Build. Print two lines:
System.out.println("ok");
System.out.println("built");
Save the file. Run javac Build.java again so Build.class is rewritten. Then run java Build. The screen shows ok, then built.
If you change the source and skip javac, java Build may still show only ok from the old .class file. That stale screen is a compiling lesson, not a Random lesson. Compile after every kept edit on this page.
public class Build {
public static void main(String[] args) {
System.out.println("ok");
System.out.println("built");
}
}
Compile with javac Build.java and run java Build again. Walk the run once. The new .class file holds both print lines. The first print writes ok. The second print writes built. Nothing waits for the keyboard. Nothing stores a seed. Nothing joins ready and 42.
Keep the two demos as two teaching shapes. First prove one line after a clean compile. Then prove two lines after a recompile. Both shapes share the same class name and the same commands. The second shape only adds another println.
If you delete Build.class and run java Build without compiling, the launcher fails because the bytecode is missing. Compile again to restore Build.class. If two public classes share one file, javac complains — keep only Build public in the teaching file.
javac versus java
These two commands look similar and do different jobs. Keep them apart while you practice.
📊 Compile and run habits for Build
| Command | Job | Beginner check |
|---|---|---|
javac Build.java |
reads source, writes Build.class |
success is often silent; look for the .class file |
java Build |
loads Build.class and starts main |
no .java ending on this command |
java Build.java |
wrong shape for this lesson | fix the command; do not treat it as the teaching run |
Skip javac after an edit |
old bytecode may still run | stale screen means compile again |
Missing Build.class |
run cannot load the class | run javac Build.java first |
Compiling is not Summary and not Mini Project
Summary asked you to explain ready 42 from Recap. Compiling asks you to watch javac write bytecode and java load it. Mini Project will ask you to build a small program of your own on the next page. Do not mix those three jobs in one beginner file today.
ok and built are only screen words for this teaching file. They are not Random faces and not a Summary map row. When the import for Random is missing on a leftover Dice file, that problem belongs to Random practice, not to Compiling. When Recap still prints ready 42, leave that file as history and keep Build on screen.
Without javac, there is no fresh .class for your latest source. Without the correct java Build command, you may never load that class. Keep both steps. Keep the class name matching the file. Keep Mini Project files off this page until that lesson.
Folder checks before you move on
List the folder after a successful compile. You should see Build.java and Build.class together. The source is what you edit. The class file is what you run. Beginners sometimes chase the screen while editing the wrong file — open Build.java, save, compile, then run.
If javac cannot find Build.java, check the current folder and the file name spelling, including capital B. If the run prints nothing, confirm main still contains the println lines. If the screen still shows only ok after you added built, you likely skipped the second compile.
Do not treat Compiling as a place to invent package trees or jar packaging. Those topics wait. This page keeps one public class, two clear screens, and the javac / java pair.
Before Mini Project
You can keep public class Build, print ok on the first demo, print ok then built on the second demo, compile with javac Build.java, see Build.class appear, and run java Build without the .java ending. The table separates compile from run. The next page is Mini Project, where you build a small program from pieces you already have.
Keep the Build demos for a clear compile-and-run pair. Keep Recap Summary files off this page when you practice Compiling. Keep Random dice demos off this page when you practice Compiling. Keep Mini Project work off this page until that lesson. Those habits are the checkpoint before you move on. When the one-line screen shows ok or the two-line screen shows ok then built, Compiling did its job for this lesson and you are ready for Mini Project.
One last pass through the folder: one public Build, Build.class present after javac, no keyboard wait, no forced Random or LocalDate imports, run command without .java. Run javac Build.java and java Build once more on the demo you keep open. If the screen still shows ok or ok then built, the Compiling shape is solid and the course can move to Mini Project on the next page. Prefer one clear compile habit per read so each pass teaches the same map.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top