1.4 Common Compilation and Runtime Errors
Prerequisites: Basic Java Syntax Rules
Estimated time: 20 minutes
You are ready to continue when: You can distinguish between errors reported by the shell, compiler, and Java launcher.
Before searching the entire error message, identify which tool reported it.
First, Distinguish Three Stages
| Stage | Example Command | Common Reporter | Explanation |
|---|---|---|---|
| Finding and starting tools | javac ... | shell | Command not found or tool cannot be executed |
| Compiling Source Code | javac -d out HelloWorld.java | javac | Issues such as syntax, type, or public class filename |
| Loading and Executing Classes | java -cp out HelloWorld | Java Launcher or Program | Classpath, entry method, runtime exceptions |
Diagnostic wording varies across JDKs, operating systems, and locales. The examples retain only the fragments needed to identify the problem; exact memorization is unnecessary.
1. Cannot Find javac
javac: command not foundThis is not a Java source code error. The shell cannot find the compiler in PATH.
command -v javac
javac --versionIf the first line does not output anything, confirm that you have installed JDK and configure PATH according to the version instructions. Do not fix the environment issues by repeatedly modifying Hello World.
2. Cannot Find Source File
error: file not found: HelloWorld.javaCheck the current directory and the actual filename:
pwd
ls -laCommon causes include different capitalization, a hidden .txt suffix, or the terminal hasn't entered the directory where the file was saved.
3. Public Classes and Filename Mismatch
Representative Diagnosis:
class HelloWorld is public, should be declared in a file named HelloWorld.javaIf the source file is actually named hello.java, fix the filename:
mv hello.java HelloWorld.java
javac -d out HelloWorld.javaThe Linux file system typically distinguishes between uppercase and lowercase letters; the default behavior on Windows is often different. Java names themselves are case-sensitive.
4. Missing Semicolon or Parenthesis
error: ';' expectedFirst, check the indicated position, then look backward to ensure the entire statement is complete. A parser can only report issues when it can no longer understand the code, so the root cause may be before the arrow.
Missing the closing right brace is common:
error: reached end of file while parsingStart from the innermost layer and check (), [], and {}, as well as whether quotes are properly closed. Do not blindly add } at the end of the file just to eliminate the error.
5. Misspelled Names: cannot find symbol and Related Diagnostics
system.out.println("Hello");system is not the standard library class System. Here, javac typically reports package system does not exist; misspelling println commonly produces cannot find symbol. Read the full diagnostic to identify the qualified name, method, or variable involved.
Similar issues:
| Typo | Correct | Type |
|---|---|---|
system | System | Class name capitalization |
string | String | Class name capitalization |
System.Out | System.out | Field name capitalization |
printl | println | Method spelling |
6. Main Class Not Found
Could not find or load main class HelloWorldCheck in order:
find out -type f -name 'HelloWorld.class' -print
java -cp out HelloWorld- No file found: Did compilation succeed, and is
-d outcorrect? - File exists: Is the classpath still pointing to
outat runtime; - Class is in a package: At startup, the fully qualified name must be used. This chapter does not use packages;
- Do not pass the
.classsuffix tojava.
ClassNotFoundException may also be caused by failures to load other classes at runtime. It is not appropriate to attribute all similar issues to "forgetting to compile".
7. Cannot Find Entry Method
Traditional Java 17 examples require:
public static void main(String[] args)Spelling the name Main or mian, omitting static, or changing the return type from void prevents the launcher from using it as a traditional Java 17 entry point.
However, both of these forms are valid:
public static void main(String words[]) { }
public static void main(String... words) { }Therefore, when debugging, check the type and modifiers, and do not misinterpret parameter variable names or bracket positions as the only legitimate form.
8. Old bytecode masks new error
javac failure does not automatically delete the previously successfully generated .class. If run directly afterward, the old program may still start, leading one to mistakenly believe the new source code has taken effect.
test ! -e out || rm -r -- out
mkdir out
javac -d out HelloWorld.java && java -cp out HelloWorld&& runs the second command only if compilation succeeds. Delete only the out in the current exercise directory. Use pwd to confirm the location, do not change the recursive delete command to a broader path.
General Debugging Loop
Reproduce the issue
↓
Log the full command, current directory, and the first error
↓
Determine whether it's during the shell, compilation, or runtime phase
↓
Modify only one clear cause
↓
Revalidate from the compilation stage onwardBefore Moving On
When you get an error, first identify whether it comes from shell, javac, or the Java launcher. Check against pwd, ls, and the artifact directory. Remember two common pitfalls: the error arrow does not guarantee to point to the original root cause, and a compilation failure will not automatically remove old .class.
Move to the next page and safely reproduce these errors, then fix them by hand.