Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we'll start by discussing the basic structure of a Java module. Does anyone know what a Java module usually includes?
I think it has some files that describe the module?
Exactly! A Java module includes a module-info.java file at its root. This file defines aspects such as what packages are exported and what other modules it requires. Can anyone remind me of the key components found in this file?
It should have 'requires' and 'exports' directives!
That's correct! The requires directive specifies dependencies, while the exports directive indicates which packages are accessible to other modules. Remember the acronym REE for 'Requires, Exports' to help you with this. Now, let's look at examples of two Java modules.
Let's discuss the actual module configurations we've set up. For the com.myapp module, our module-info.java states that it requires com.utils. Why do you think we set it up that way?
Because com.myapp needs to use features from com.utils!
Exactly! The requires com.utils; line ensures that any classes or resources in com.utils can be accessed by com.myapp. Let's also take a look at the export statement for com.myapp. What does it help achieve?
It allows other modules to access the packages inside com.myapp.
Spot on! This encapsulation helps maintain control over the API. Finally, can someone tell me what is defined in com.utils's module-info.java?
It exports the com.utils packages!
Correct! This two-way interaction through module definitions is essential for reusable software components. Let's summarize what we learned.
Today we discussed the importance of module configurations and how dependencies and exports facilitate code structure. Remember the importance of encapsulation!
All right! Now let's talk about how to compile these modules. First, can anyone show me the command we use to compile the modules?
I think it's something like javac -d out --module-source-path src. Is that right?
Yes, that's correct! This command compiles all Java files and keeps the module structure intact. You might also notice that we use $(find src -name '*.java'). What does that part of the command do?
It locates all Java files in the specified source path, right?
Exactly! Now, after compiling, how do we run the application?
We use java --module-path out -m com.myapp/com.myapp.Main!
Exactly correct! This indicates the module and the entry point. Let's wrap up today's lessons.
In summary, we covered the compile command and how to run our Java modules effectively. Understanding these commands is key to working with JPMS.
A self-contained unit of code that groups related packages and resources.
The file that describes module dependencies and exports.
Indicates dependency on other modules.
Controls access to packages by other modules.
Specifies where to find modules in the file system.
When your code needs to share, exports make it fair, but don't expose all — be aware!
Imagine a library where each shelf has its sign. Some shelves are open for borrowing, while others are for staff only. This is like packages in modules; only those that are exported can be accessed by others!
R.E.E for Remembering; Requires, Exports, Encapsulated is the way!
M.R.E.S. stands for Module, Requires, Exports, Structure.
Term
What is the purpose of module-info.java?
Click to flip
Term
What does the 'exports' directive accomplish?
Click to flip
A special Java file used to define a module's name, dependencies, and exported packages.
A directive in module-info.java that specifies other modules that the current module needs.
A directive in module-info.java that defines which packages are accessible to other modules.
A path that tells the compiler and the Java runtime where to find the compiled modules.
A self-contained unit of code in Java that encapsulates packages and resources.
This section demonstrates how to create and utilize Java modules, detailing the folder structure and example module configurations.
In this section, we explore the practical aspects of creating and using Java modules. We present a sample folder structure and demonstrate how to configure module-info.java files for two different modules: com.myapp and com.utils. Additionally, we provide the necessary compile and run commands for the created modules.
In this section, we delve into the practical implementation of Java modules within the Java Platform Module System (JPMS). We establish a foundational understanding needed to create effective modules in Java applications.
The correct organization of module files is pivotal for clarity and functionality. The following is a suggested folder structure:
src/
├── com.myapp/
│ ├── module-info.java
│ └── com/myapp/Main.java
├── com.utils/
│ ├── module-info.java
│ └── com/utils/Utils.javaBelow, we outline the configurations for module-info.java for both modules.
com.myapp module:module com.myapp {
requires com.utils;
exports com.myapp;
}com.utils module:module com.utils {
exports com.utils;
}These files define module dependencies and the packages that are made accessible to other modules, addressing encapsulation effectively.
To compile the modules, use the following command:
javac -d out --module-source-path src $(find src -name "*.java")
This command ensures that all Java files in the specified source path are compiled correctly into the output directory.
To run the application, utilize the command:
java --module-path out -m com.myapp/com.myapp.Main
This indicates the main module and entry point for execution.
This section highlights the core process of setting up modules in Java and reinforces how JPMS facilitates better structuring and reusability of code.