When Code Will Not Run
- Terminology and concepts to look out for: syntax · error message · debugging
- Relevance to me: Every Java programmer sees error messages, and reading the message is the first step in fixing the problem. Today you learn what the compiler is telling you when your code will not run.
- Board Question: The compiler stops with the message Stops.java:4: error: ';' expected. Which file and which line would you open first? What do you think is missing?
Open the discussion form on the right and answer Section 1 from your first reading.
Syntax, Error Messages and Debugging
This section explains the rules a Java program must follow, how to read the message the compiler prints when a rule is broken, and how a programmer fixes the problem.
The Rules for Writing Code
Syntax is the set of rules a programmer follows when writing code. In Java, a statement such as a line that prints ends with a semicolon. Every opening brace, `{`, needs a matching closing brace, `}`, because braces mark where a block of code begins and ends. Text sits inside a pair of double quotes, as in `"Canal Street"`. Java also treats a capital letter and a small letter as different characters, so `System` must start with a capital S. A reader of English can still follow a sentence with a missing period, but the compiler will not compile a program with a missing semicolon. Syntax is only about how code is written, so a program can follow every rule and still print the wrong answer.
Reading an Error Message
As an earlier lesson explained, the javac compiler turns source code into bytecode in a .class file. It checks that the program follows the syntax rules. When a rule is broken, javac makes no .class file and prints an error message instead, which is a report that names the file, the line and the problem the compiler noticed. The first line of a javac error message has three parts, separated by colons: the file, the line number and what went wrong. Under that line, javac prints the line of code it was reading and puts a caret, `^`, under the spot where it noticed the problem. Figure 1 shows a real message, `Stops.java:4: error: ';' expected`, with each part labeled. The file is Stops.java, the line is 4, and javac expected a semicolon at the end of `System.out.println(stops)`. An error message names the file, the line and what the compiler expected, but it does not always name the fix.
When the Line Is Not the Mistake
The line in an error message is where the compiler noticed the problem, which is not always where the mistake is. Each program below fails on purpose, and its first line is a comment. The compiler skips everything from `//` to the end of that line, but the comment still counts as line 1. The next program is missing the closing brace for the class.
1// This does not compile. 2public class Stops { 3 public static void main(String[] args) { 4 System.out.println("Next stop: Canal Street"); 5 }
Stops.java:5: error: reached end of file while parsing
}
^
1 error
The message points at line 5, which is the last line of the file. The mistake is a brace that was never written, and javac noticed only when it ran out of code to read.
Some messages name what the compiler saw instead of the fix. The program below writes `system` with a small s.
1// This does not compile. 2public class Stops { 3 public static void main(String[] args) { 4 system.out.println("Next stop: Canal Street"); 5 } 6}
Stops.java:4: error: package system does not exist
system.out.println("Next stop: Canal Street");
^
1 error
Because a small s and a capital S are different characters, javac reads `system` as a different name from `System`, and it reports that no package called system exists. The message never says to use a capital S, so the programmer works out the fix from what the message describes.
Finding and Fixing a Problem
Debugging is finding the cause of a problem, fixing it and running the code again to check the fix. An OpenStax computer science textbook advises fixing only the first error message and then compiling again, because one syntax error can cause false alarms later in the code. For example, a programmer reads the message in Figure 1, opens Stops.java at line 4, adds the semicolon and compiles again. If a new message appears after that fix, the debugging is not finished yet. Changing lines at random until the message goes away is not debugging, because it does not find the cause or check the fix.
Code with no syntax errors can still be wrong. A program that compiles and prints 4 when it should print 5 has a mistake the compiler cannot see, because every rule was followed. A test with an expected output catches this kind of mistake, as the lesson on testing a change showed. The programmer runs the program, compares what it prints with what it should print, and looks for the cause when the two differ. Debugging covers both kinds of problems, the ones the compiler reports and the ones that only a test shows.
Summary
Syntax is the set of rules a programmer follows when writing Java code. When a rule is broken, javac makes no .class file and prints an error message that names the file, the line and what it expected. Debugging means finding the cause, fixing one thing and then compiling or testing again to check the fix.
Sources: Oracle, Common Problems (and Their Solutions) (n.d.); Oracle, About the Java Technology (n.d.); Oracle, The javac Command (n.d.); Oracle, The Java Language Specification, SE 21 (n.d.); Franchitti, Introduction to Computer Science (2024); course JDK, Stops.java (2026).
-
notebook cue: syntax
In Figure 1, the program is missing one character. Which character is it, and which syntax rule does that break?
Check
The missing character is a semicolon at the end of System.out.println(stops). It breaks the syntax rule that a Java statement ends with a semicolon, so javac stopped and made no Stops.class. See The Rules for Writing Code, paragraph 1, and Figure 1.
-
notebook cue: error message
An error message begins Stops.java:5: error. What do the two parts before the word error tell you?
Check
Stops.java is the file to open, and 5 is the line where javac noticed the problem. The line is where the compiler noticed, which is not always where the mistake is, as with a missing closing brace. See Reading an Error Message and When the Line Is Not the Mistake.
-
notebook cue: debugging
A program compiles and runs, but it prints 4 when it should print 5. Is that a syntax error? What would debugging it look like?
Check
It is not a syntax error, because the program compiled, so every rule was followed. Debugging it means running a test with the expected output 5, finding the line that causes the 4, fixing it and running the test again to check the fix. See Finding and Fixing a Problem, paragraph 2.
-
notebook cue: syntax
In Figure 1, the program is missing one character. Which character is it, and which syntax rule does that break?
Check
The missing character is a semicolon at the end of System.out.println(stops). It breaks the syntax rule that a Java statement ends with a semicolon, so javac stopped and made no Stops.class. See The Rules for Writing Code, paragraph 1, and Figure 1.
-
notebook cue: error message
An error message begins Stops.java:5: error. What do the two parts before the word error tell you?
Check
Stops.java is the file to open, and 5 is the line where javac noticed the problem. The line is where the compiler noticed, which is not always where the mistake is, as with a missing closing brace. See Reading an Error Message and When the Line Is Not the Mistake.
-
notebook cue: debugging
A program compiles and runs, but it prints 4 when it should print 5. Is that a syntax error? What would debugging it look like?
Check
It is not a syntax error, because the program compiled, so every rule was followed. Debugging it means running a test with the expected output 5, finding the line that causes the 4, fixing it and running the test again to check the fix. See Finding and Fixing a Problem, paragraph 2.
Syntax, Error Messages and Debugging
This section explains the rules a Java program must follow, how to read the message the compiler prints when a rule is broken, and how a programmer fixes the problem.
The Rules for Writing Code
Syntax is the set of rules a programmer follows when writing code. In Java, a statement such as a line that prints ends with a semicolon. Every opening brace, `{`, needs a matching closing brace, `}`, because braces mark where a block of code begins and ends. Text sits inside a pair of double quotes, as in `"Canal Street"`. Java also treats a capital letter and a small letter as different characters, so `System` must start with a capital S. A reader of English can still follow a sentence with a missing period, but the compiler will not compile a program with a missing semicolon. Syntax is only about how code is written, so a program can follow every rule and still print the wrong answer.
Reading an Error Message
As an earlier lesson explained, the javac compiler turns source code into bytecode in a .class file. It checks that the program follows the syntax rules. When a rule is broken, javac makes no .class file and prints an error message instead, which is a report that names the file, the line and the problem the compiler noticed. The first line of a javac error message has three parts, separated by colons: the file, the line number and what went wrong. Under that line, javac prints the line of code it was reading and puts a caret, `^`, under the spot where it noticed the problem. Figure 1 shows a real message, `Stops.java:4: error: ';' expected`, with each part labeled. The file is Stops.java, the line is 4, and javac expected a semicolon at the end of `System.out.println(stops)`. An error message names the file, the line and what the compiler expected, but it does not always name the fix.
When the Line Is Not the Mistake
The line in an error message is where the compiler noticed the problem, which is not always where the mistake is. Each program below fails on purpose, and its first line is a comment. The compiler skips everything from `//` to the end of that line, but the comment still counts as line 1. The next program is missing the closing brace for the class.
1// This does not compile. 2public class Stops { 3 public static void main(String[] args) { 4 System.out.println("Next stop: Canal Street"); 5 }
Stops.java:5: error: reached end of file while parsing
}
^
1 error
The message points at line 5, which is the last line of the file. The mistake is a brace that was never written, and javac noticed only when it ran out of code to read.
Some messages name what the compiler saw instead of the fix. The program below writes `system` with a small s.
1// This does not compile. 2public class Stops { 3 public static void main(String[] args) { 4 system.out.println("Next stop: Canal Street"); 5 } 6}
Stops.java:4: error: package system does not exist
system.out.println("Next stop: Canal Street");
^
1 error
Because a small s and a capital S are different characters, javac reads `system` as a different name from `System`, and it reports that no package called system exists. The message never says to use a capital S, so the programmer works out the fix from what the message describes.
Finding and Fixing a Problem
Debugging is finding the cause of a problem, fixing it and running the code again to check the fix. An OpenStax computer science textbook advises fixing only the first error message and then compiling again, because one syntax error can cause false alarms later in the code. For example, a programmer reads the message in Figure 1, opens Stops.java at line 4, adds the semicolon and compiles again. If a new message appears after that fix, the debugging is not finished yet. Changing lines at random until the message goes away is not debugging, because it does not find the cause or check the fix.
Code with no syntax errors can still be wrong. A program that compiles and prints 4 when it should print 5 has a mistake the compiler cannot see, because every rule was followed. A test with an expected output catches this kind of mistake, as the lesson on testing a change showed. The programmer runs the program, compares what it prints with what it should print, and looks for the cause when the two differ. Debugging covers both kinds of problems, the ones the compiler reports and the ones that only a test shows.
Summary
Syntax is the set of rules a programmer follows when writing Java code. When a rule is broken, javac makes no .class file and prints an error message that names the file, the line and what it expected. Debugging means finding the cause, fixing one thing and then compiling or testing again to check the fix.
Sources: Oracle, Common Problems (and Their Solutions) (n.d.); Oracle, About the Java Technology (n.d.); Oracle, The javac Command (n.d.); Oracle, The Java Language Specification, SE 21 (n.d.); Franchitti, Introduction to Computer Science (2024); course JDK, Stops.java (2026).
Each table names the file, the line and the missing character, and agrees on what to do right after fixing it.
Chromebooks stay closed while your table talks. Sections 1 and 2 of the discussion form are already done. After you talk, finish Section 3 right here under My View. Submit before the exit ticket.
This form has three sections. Section 1: answer today's Board Question. Section 2: after the reading. Section 3 opens later in class. Today's Board Question: The compiler stops with the message Stops.java:4: error: ';' expected. Which file and which line would you open first? What do you think is missing?
CR 1. Board Question: The compiler stops with the message Stops.java:4: error: ';' expected. Which file and which line would you open first? What do you think is missing? Give your answer and one reason.
Write your answer in your lab notebook, labeled CR 1.
MC 1. Think about your answer to the board question. The compiler stops with the message Stops.java:4: error: ';' expected. Which file and which line would you open first? What do you think is missing? Which position is closest to yours now?
- Line 4 of Stops.java, because a semicolon is missing at the end of that line.
- Line 1, because a program is always fixed from the top.
- Any line, because the message is too short to tell you anything.
MC 2. (Reading claim 1) When javac finds a syntax error, it still makes a .class file.
- Agree
- Disagree
MC 3. (Reading claim 2) The line in an error message is where javac noticed the problem, which is not always where the mistake is.
- Agree
- Disagree
MC 4. (Reading claim 3) Code with no syntax errors always prints the right answer.
- Agree
- Disagree
MC 5. (Reading claim 4) In Figure 1, the caret sits just past the closing parenthesis, at the spot where javac noticed that the semicolon was missing.
- Agree
- Disagree
MC 6. (Reading claim 5) The message package system does not exist tells the programmer to change the small s to a capital S.
- Agree
- Disagree
MC 7. (Reading claim 6) Changing lines at random until the error message goes away is one way of debugging.
- Agree
- Disagree
MC 8. (Reading claim 7) A computer science textbook advises fixing only the first error message and then compiling again.
- Agree
- Disagree
MC 9. (Reading claim 8) In Reading 2, the line System.out.println("Total: " + 5 + 3); prints Total: 8.
- Agree
- Disagree
CR 2. (A different view) Write the view at your table that was most different from yours. If everyone agreed, write the strongest argument against your table's answer.
Write your answer in your lab notebook, labeled CR 2.
CR 3. Think about the Board Question again: The compiler stops with the message Stops.java:4: error: ';' expected. Which file and which line would you open first? What do you think is missing? Do you have the same opinion as when you first answered the board question? If it is the same, what from the reading or table talk reaffirmed your opinion? If it is different, what from the reading or table talk changed your opinion?
Write your answer in your lab notebook, labeled CR 3.