Sub topic 12
Compiling and Running Packaged Java Code
Prerequisites and project layout
This chapter assumes that you can write a Java class with a main method, compile a basic program, and use a command line. You should also understand classes, methods, and fields.
A package declaration gives a class its fully qualified name. The package name should match the directory path below the source root:
shop-project/
├── src/
│ └── com/
│ └── example/
│ └── shop/
│ ├── Main.java
│ └── model/
│ └── Product.java
└── out/src is the source root. The out directory will hold compiled .class files.
Compile a packaged program
Product.java declares the com.example.shop.model package:
package com.example.shop.model;
public class Product {
private final String name;
private final double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}Main.java belongs to a different package and imports 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", 1.50);
System.out.println(product.getName() + " costs " + product.getPrice());
}
}From the project root, compile both source files:
mkdir -p out
javac -d out src/com/example/shop/Main.java src/com/example/shop/model/Product.javaThe -d out option tells javac where to write class files. The compiler creates directories that match the packages:
out/
└── com/
└── example/
└── shop/
├── Main.class
└── model/
└── Product.classThe directory structure helps the Java runtime locate classes. It does not replace the package declaration. Both the declaration and the compiled path matter.
Run the packaged class
Run the program with:
java -cp out com.example.shop.MainThe -cp option sets the classpath. A classpath is a list of locations where Java searches for compiled classes and other resources. Here, out is the classpath root.
Use the fully qualified name, including the package, when starting the program. Use dots between name parts. Do not write Main, com/example/shop/Main, or Main.class.
| Command part | Meaning |
|---|---|
| `java` | Starts the Java launcher |
| `-cp out` | Searches for classes below `out` |
| `com.example.shop.Main` | Names the class containing `main` |
The program prints:
Pen costs 1.5Common errors
Could not find or load main classusually means the classpath or fully qualified name is wrong.ClassNotFoundExceptionoften means a required compiled class is missing from the classpath.package ... does not existduring compilation usually means a source file was omitted or the source paths are wrong.- Running
java -cp src ...fails when only source files exist. Compile the files first, then point Java at the class-file directory.
For larger projects, build tools such as Maven or Gradle calculate source paths, classpaths, and output directories for you. The underlying rule remains the same: package names identify classes, and classpath roots identify where those package paths begin.
Exercise
From the project root, what command compiles the two example files into out, and what command runs Main?
Show answer
Compile with javac -d out src/com/example/shop/Main.java src/com/example/shop/model/Product.java, then run with java -cp out com.example.shop.Main.