Mapping Packages to Directory Structure
What you'll learn: How Java package names directly correspond to folder paths on your file system.
The Core Rule
When you declare a package in Java, you're not just organizing code logically—you're creating a physical requirement for how your files must be stored. The package name becomes the directory path.
If your class declares:
package com.example.app;
Then your .java file must live inside this folder structure:
com/example/app/
Every dot (.) in the package name represents a subdirectory separator (/ on Unix/Linux/Mac, \ on Windows).
The Analogy
Think of packages like postal addresses. If you write "123 Main Street, Springfield, Illinois" on an envelope, the mail carrier expects that exact physical location to exist. Similarly, when Java sees com.example.app, it expects to navigate through folders: first com, then example, then app.
Concrete Example
For a class declared as:
package com.example.app;
public class Calculator {
// class body
}
The file Calculator.java must exist at:
com/
example/
app/
Calculator.java
If the directory structure doesn't match, the compiler and runtime won't find your class, resulting in errors like class not found or package does not exist.
Why This Matters
This strict mapping prevents naming collisions and allows Java to locate classes efficiently. The file system becomes a hierarchical index that mirrors your package hierarchy.
Key Takeaway: Package names map directly to directory paths—com.example.app requires the folder structure com/example/app/, with each dot representing a directory separator.