Skip to content

Lab: compiling and running packaged classes

☕ Java 17 · Organizing Your Code with Packages and Imports · 3/3

Goal: build a small project with two packages and run it from the terminal, without an IDE.

Setup: in an empty folder (the project root), create this tree:

project-root/
└── com/netflix/
    ├── content/
    │   ├── Movie.java
    │   └── Series.java
    └── platform/
        └── StreamingPlatform.java

Open a terminal in the project root and stay there for every command.

Step 1 — The content classes

com/netflix/content/Movie.java:

package com.netflix.content;

public class Movie {
    private String title;
    private int releaseYear;

    public Movie(String title, int releaseYear) {
        this.title = title;
        this.releaseYear = releaseYear;
    }

    @Override
    public String toString() {
        return "Movie: " + title + " (" + releaseYear + ")";
    }
}

com/netflix/content/Series.java:

package com.netflix.content;

public class Series {
    private String title;
    private int numberOfSeasons;

    public Series(String title, int numberOfSeasons) {
        this.title = title;
        this.numberOfSeasons = numberOfSeasons;
    }

    @Override
    public String toString() {
        return "Series: " + title + " - " + numberOfSeasons + " season(s)";
    }
}

The package line matches the folder. The constructor and toString() are covered in later sections: here, toString() decides how an object prints.

Step 2 — The platform class

com/netflix/platform/StreamingPlatform.java:

package com.netflix.platform;

import com.netflix.content.Movie;
import com.netflix.content.Series;

public class StreamingPlatform {
    public static void main(String[] args) {
        Movie movie = new Movie("Inception", 2010);
        Series series = new Series("Stranger Things", 4);

        System.out.println("=== Netflix Streaming Platform ===");
        System.out.println(movie);
        System.out.println(series);
        System.out.println("Platform is ready to stream!");
    }
}

Movie and Series are in another package, so they're imported.

Step 3 — Compile from the project root

javac takes file paths, with slashes:

$ javac com/netflix/content/*.java
$ javac com/netflix/platform/StreamingPlatform.java

Each .class file appears next to its .java file: the compiled classes mirror the folder structure.

Step 4 — Run with the fully qualified name

java takes the class name, with dots:

$ java com.netflix.platform.StreamingPlatform
=== Netflix Streaming Platform ===
Movie: Inception (2010)
Series: Stranger Things - 4 season(s)
Platform is ready to stream!

Java turns the dots into the folder path com/netflix/platform and loads the class from there.

Step 5 — The classic mistake

Run the same command from inside com/:

$ cd com
$ java com.netflix.platform.StreamingPlatform
Error: Could not find or load main class com.netflix.platform.StreamingPlatform

Java now looks for com/com/netflix/..., which doesn't exist. Go back to the project root and it works again.

Full code on GitHub

← Imports · Back to the series


🎓 Preparing the OCP Java 17 exam? Exam traps and practice questions for this section → (optional)