1.5 Lab: Fix Errors, Change Output, Test Entry Points
Prerequisites: Lessons 1 through 4 of this chapter Estimated time: 30 minutes Practice environment: JDK 17+ and Bash; all files written to a temporary directory You are ready to continue when: You have completed three required experiments and save the commands and results
All exercises are performed in a new directory:
chapter1_lab_root=$(mktemp -d)
readonly chapter1_lab_root
mkdir -p "$chapter1_lab_root/chapter01-lab/out"
cd "$chapter1_lab_root/chapter01-lab"When deleting or rebuilding artifacts, operate only within the out.
Required Exercise 1: Fix Three Types of Compilation Errors
Save as Bug.java:
public class Bug {
public static void main(String[] args) {
system.out.println("Hello, Java!")
System.Out.println("Hello Coder!");
}
}Compile the code exactly as written:
javac -d out Bug.javaRecord the first diagnostic message, then fix the issues in order:
- Add a semicolon at the end of the first method call;
- Fix the capitalization of
system; - Fix the capitalization of
Out.
Recompile after each change. Finally, run:
java -cp out BugExpected output:
Hello, Java!
Hello Coder!Note: A single syntax error can trigger multiple cascading diagnostics, so the number of initial error messages may not exactly match the number of independent root causes in the source code.
Required Exercise 2: Observe Recompilation
Save as Greeting.java:
public class Greeting {
public static void main(String[] args) {
System.out.println("version one");
}
}javac -d out Greeting.java
java -cp out GreetingChange the source code to version two, but do not compile yet, run it directly. You should still see version one. Only after recompiling and running again will the output change to version two.
In your lab notes, write a sentence explaining: Which file is being executed by java?
Required Exercise 3: Using Command-Line Arguments
Save as Greeter.java:
public class Greeter {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Hello, user!");
return;
}
System.out.println("Hello, " + args[0] + "!");
}
}Compile and run twice:
javac -d out Greeter.java
java -cp out Greeter
java -cp out Greeter LinExpected output:
Hello, user!
Hello, Lin!You haven't yet systematically studied if and arrays, but for now, just confirm that the words following the class name in the startup command are passed into args.
Optional Exercise 1: print, println, and Escape Sequences
public class Lines {
public static void main(String[] args) {
System.out.print("north ");
System.out.println("gate");
System.out.println("line 1\nline 2");
System.out.println("A\tB");
}
}Before running the code, write down your prediction of the four output lines. Then compile and verify. The terminal's display width for tab characters may vary, so focus only on whether there is a tab spacing between A and B.
Optional Exercise 2: Can Entry Methods Be Overloaded?
Save as EntryPoints.java:
public class EntryPoints {
public static void main(String[] args) {
System.out.println("launcher entry");
main(3);
}
public static void main(int count) {
System.out.println("overload: " + count);
}
}It compiles because the two methods have different parameter types, which qualifies as method overloading. The Java launcher selects the traditional entry point with a string array parameter, while main(int) is a regular overloaded method called internally by the program.
Now copy main(String[] args) and keep the signature exactly the same. The compiler will reject duplicate methods within the same class. This demonstrates the distinction between "same name" and "same signature."
Optional Exercise 3: Clean Artifacts Before Verification
pwd
test ! -e out || rm -r -- out
mkdir out
javac -d out Greeter.java
java -cp out Greeter AdaThis demonstrates that the results are not dependent on stale artifacts. pwd must show the current experiment directory; do not run recursive deletion in an uncertain or ambiguous location.
Experiment Log Template
| Experiment | Original Command | First Key Diagnosis or Output | Modification | Revalidation Result |
|---|---|---|---|---|
| Bug | ||||
| Rebuild | ||||
| Parameters |
Acceptance and Cleanup
Confirm that Bug.java produces two lines of output, observe firsthand that the old bytecode still runs, and verify that both the zero-argument and parameterized paths of Greeter pass successfully. After saving your lab notes somewhere you want to keep them, delete the temporary directory:
cd
ls -ld -- "$chapter1_lab_root"
rm -r -- "$chapter1_lab_root"Errors don't teach themselves; you must document the commands, stages, the first diagnostic message, and the location of the output artifact, then re-run the test to narrow down the issue.