Sub topic 4
Matching Packages to Directories
Assume you can write a basic Java class, compile it from a command line, and use classes, methods, and fields. This chapter focuses on how a package declaration maps to source and compiled-class directories.
The package directory rule
A Java package is a namespace that groups related classes. The package name appears in the source file:
package com.example.shop.model;
public class Product {
private final String name;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
}The package name is split at each dot. Each part becomes one directory:
src/
└── com/
└── example/
└── shop/
└── model/
└── Product.javaThe file name must match the public class name. Therefore, the public class Product belongs in Product.java.
The source root is the directory above the first package directory. In this example, src is the source root. The package path begins at com.
Source paths and class-file paths
The Java compiler reads source files from their source paths. It writes compiled .class files to a class output directory. The -d option sets that output directory.
For example, this command compiles Product.java and places the result under out:
javac -d out src/com/example/shop/model/Product.javaThe compiler creates this path:
out/
└── com/
└── example/
└── shop/
└── model/
└── Product.classThe output directory does not replace the package path. It becomes the root under which the compiler creates the package directories.
| Item | Example |
|---|---|
| Package declaration | `com.example.shop.model` |
| Source file | `src/com/example/shop/model/Product.java` |
| Class file | `out/com/example/shop/model/Product.class` |
| Fully qualified class name | `com.example.shop.model.Product` |
Compiling a program with multiple packages
Suppose Main uses Product:
package com.example.shop;
import com.example.shop.model.Product;
public class Main {
public static void main(String[] args) {
Product product = new Product("Pen");
System.out.println(product.getName());
}
}The files are arranged like this:
src/
└── com/
└── example/
└── shop/
├── Main.java
└── model/
└── Product.javaCompile both files into the same output root:
javac -d out \
src/com/example/shop/Main.java \
src/com/example/shop/model/Product.javaThen run Main using its fully qualified name. The classpath points to the directory containing the package root:
java -cp out com.example.shop.MainDo not pass src/com/example/shop/Main.java to java. The java command runs compiled classes, not source paths. Also do not include com.example.shop.Main.java as the class name. Java uses dots between package components and the class name.
Common directory mistakes
- Placing
Product.javadirectly insrcwhile declaringpackage com.example.shop.model. - Naming a directory
com.example.shop.modelinstead of using nested directories. - Running
java -cp out Maininstead of usingcom.example.shop.Main. - Setting the classpath to
out/com/example/shopwhen the class name iscom.example.shop.Main. The classpath should point toout.
Exercise
If a class declares package org.example.billing, where should Invoice.java be placed under the source root, and what class name should you use to run it?
Show answer
Place it at org/example/billing/Invoice.java. If it has a main method, run it with the fully qualified name org.example.billing.Invoice.