Classpath &
Directory Structure
Learn how Java locates your classes at compile time and runtime through packages, directory hierarchies, and the classpath.
What is a Package?
A package is Java's way of grouping related classes into a namespace. Think of it like a folder system for your code: it prevents name collisions and keeps projects organized.
Packages Map to Directories
Every dot in a package name corresponds to a subdirectory on disk. The compiler and JVM enforce this mapping: if the directory structure doesn't match, your code won't compile or run.
What is the Classpath?
The classpath is a list of locations (directories or JAR files) where the Java compiler and JVM look for .class files. Without a correct classpath, Java cannot find your compiled classes.
Why Does This Matter?
Understanding classpath and directory structure is essential for every Java developer. Without it, you'll hit frustrating errors and struggle with project organization.
The Core Rule
Setting the Classpath
There are three common ways to tell the JVM where to look for classes. Each method sets the same underlying search path.
java -cp build/classes Main
export CLASSPATH=./build
Classpath vs Package
These two concepts work together but serve different roles. Confusing them is one of the most common sources of errors.
| Classpath | Package | |
|---|---|---|
| What is it? | A search path (list of roots) | A namespace (logical grouping) |
| Defined by | -cp flag, env variable, or default | The package keyword in source |
| Maps to | Root directories or JAR files | Subdirectory hierarchy |
| Changes at | Compile time / runtime config | Design time (in source code) |
| Example | -cp ./build | package com.app; |
How to Set Up a Project
Example 1: Simple Project
A single-package project with one class. This is the simplest possible structure.
Example 2: Multi-Package Project
A project with two packages where one class imports another. The classpath must include both packages' root.
Example 3: Adding JAR Libraries
A JAR file is just a ZIP archive of compiled .class files with preserved directory structure. Adding it to the classpath lets the JVM look inside the JAR.
How the JVM Resolves an Import
The Default Package
When a .java file has no package declaration, it belongs to the default (unnamed) package. This is fine for quick experiments, but causes real problems in larger projects.
Common Mistakes
Summary Reference
| Concept | What it is | Example |
|---|---|---|
| Package | Logical namespace for classes | com.school.model |
| Directory | Physical folder mirroring package | com/school/model/ |
| Classpath | Search path for .class files | -cp build:lib/*.jar |
| JAR | ZIP of compiled .class files | gson-2.10.jar |
| FQN | Fully Qualified Class Name | com.school.model.Student |
| Default package | Unnamed package (no declaration) | Avoid in production |
Question 1 of 5
A Java file declares package org.data.io; at the top. Which directory must it be placed in (relative to the source root)?
Question 2 of 5
You compiled your project with javac -d build src/com/app/Main.java. What is the correct command to run it?
Question 3 of 5
If both -cp and the CLASSPATH environment variable are set, which one does the JVM use?
Question 4 of 5
A class in the default package (no package declaration) wants to import com.util.Helper. Another class in com.util wants to import the default-package class. Which statement is true?
Question 5 of 5
Your project compiles successfully, but at runtime you get ClassNotFoundException for a class inside lib/gson.jar. What is the most likely cause?