01 / 21

Classpath &
Directory Structure

Learn how Java locates your classes at compile time and runtime through packages, directory hierarchies, and the classpath.

Java

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.

Namespace
Two classes can share the same name if they live in different packages.
Organization
Group related classes together: util, model, service.
Access Control
Package-private visibility restricts access to classes in the same package.

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.

com.school.grades com/ school/ grades/ ReportCard.java ← "com" ← "school" ← "grades"

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.

JVM CLASSPATH src/ (source directory) build/classes/ (output directory) lib/utils.jar (JAR archive) ✓ Class found!

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.

1
Compilation: The compiler uses the classpath to resolve import statements and find dependent classes.
2
Runtime: The JVM uses the classpath to load .class files when your program actually runs.
3
Collaboration: Proper package structure lets teams work on large codebases without naming conflicts.
4
Libraries: Third-party JARs are useless if they're not on the classpath: the JVM simply can't see them.

The Core Rule

com.example.Foo Fully Qualified Name resolves to {CP_ROOT}/com/example/Foo.class File System Path Package: com.example Class: Foo Extension: .class dots → slashes, then append .class This is the one rule that governs everything.

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.

-cp flag
Pass directly on the command line:
java -cp build/classes Main
CLASSPATH env
Set an environment variable:
export CLASSPATH=./build
Default (.)
If nothing is set, Java searches the current directory only.

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

1
Choose a root directory for your source files (e.g., src/).
2
Create subdirectories that match each package name: dots become slashes.
3
Add the package declaration as the first line in every .java file.
4
Compile from the root: javac -d build src/com/app/Main.java
5
Run with the classpath pointing to the output: java -cp build com.app.Main

Example 1: Simple Project

A single-package project with one class. This is the simplest possible structure.

myproject/ ├── src/ │ └── com/ │ └── hello/ │ └── Greeter.java └── build/ └── com/ └── hello/ └── Greeter.class
// Greeter.java package com.hello; public class Greeter { public static void main(String[] args) { System.out.println("Hello, World!"); } }

Example 2: Multi-Package Project

A project with two packages where one class imports another. The classpath must include both packages' root.

school-app/ ├── src/ │ └── com/ │ └── school/ │ ├── model/ │ │ └── Student.java │ └── app/ │ └── Main.java └── build/
// Main.java package com.school.app; import com.school.model.Student; public class Main { public static void main(String[] args) { Student s = new Student("Alice"); } }

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.

webapp/ ├── src/ │ └── com/ │ └── webapp/ │ └── App.java ├── build/ └── lib/ └── gson-2.10.jar
// Compile with JAR on classpath javac -cp lib/gson-2.10.jar -d build \ src/com/webapp/App.java // Run with both build dir and JAR java -cp build:lib/gson-2.10.jar \ com.webapp.App

How the JVM Resolves an Import

import com.util.Helper; Convert: com/util/Helper.class Search each classpath entry in order ./build/ ✕ not found ./lib/utils.jar ✓ FOUND ./other/ (skipped) Class loaded successfully

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.

Cannot import
Classes in named packages cannot import classes from the default package.
Name collisions
Without a namespace, two classes with the same name will conflict immediately.

Common Mistakes

✕ Wrong directory
Putting Foo.java in src/ directly when it declares package com.app. It must be in src/com/app/.
✕ Running with class name only
Typing java Foo instead of java com.app.Foo. You must use the fully qualified name.
✕ Classpath points too deep
Setting -cp build/com/app instead of -cp build. The classpath must point to the root, not the package folder.
✕ Missing JAR on classpath
Compiling with a JAR but forgetting to include it at runtime: compiles fine, crashes on launch.

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?