Chapter 3. Working with Modules
In this chapter, we take the first steps towards modular development using Java 9. Instead of looking at existing modules in the JDK, itâs time to get your hands dirty by writing your first module. To start things off easily, we turn our attention to the simplest possible module. Letâs call it our Modular Hello World. Armed with this experience, weâll then be ready to take on a more ambitious example with multiple modules. At that point, weâll introduce the running example to be used throughout this book, called EasyText. Itâs designed to gradually grow with you, as you learn more about the module system.
Your First Module
Youâve seen examples of module descriptors in the previous chapter. A module is generally more than just a descriptor, though. The Modular Hello World therefore transcends the level of a single source file: we need to examine it in context. Weâll start by compiling, packaging, and running a single module to get acquainted with the new tooling options for modules.
Anatomy of a Module
Our goal for this first example is to compile the following class into a module and run it (ExampleÂ 3-1). We start out with a single class in a package, leading to a single module. Modules may contain only types that are inside packages, so a package definition is required.
Example 3-1. HelloWorld.java (â¥ chapter3/helloworld)