Skip to content

VOL 01 / CH 01 / LESSON 04

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 ​

StageExample CommandCommon ReporterExplanation
Finding and starting toolsjavac ...shellCommand not found or tool cannot be executed
Compiling Source Codejavac -d out HelloWorld.javajavacIssues such as syntax, type, or public class filename
Loading and Executing Classesjava -cp out HelloWorldJava Launcher or ProgramClasspath, 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 ​

text
javac: command not found

This is not a Java source code error. The shell cannot find the compiler in PATH.

bash
command -v javac
javac --version

If 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 ​

text
error: file not found: HelloWorld.java

Check the current directory and the actual filename:

bash
pwd
ls -la

Common 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:

text
class HelloWorld is public, should be declared in a file named HelloWorld.java

If the source file is actually named hello.java, fix the filename:

bash
mv hello.java HelloWorld.java
javac -d out HelloWorld.java

The 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 ​

text
error: ';' expected

First, 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:

text
error: reached end of file while parsing

Start 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.

java
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:

TypoCorrectType
systemSystemClass name capitalization
stringStringClass name capitalization
System.OutSystem.outField name capitalization
printlprintlnMethod spelling

6. Main Class Not Found ​

text
Could not find or load main class HelloWorld

Check in order:

bash
find out -type f -name 'HelloWorld.class' -print
java -cp out HelloWorld
  • No file found: Did compilation succeed, and is -d out correct?
  • File exists: Is the classpath still pointing to out at 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 .class suffix to java.

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:

java
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:

java
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.

bash
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 ​

text
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 onward

Before 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.

→ Next: Hello World Experiment

Built with VitePress | Software Systems Atlas