13.7 - Creating and Using Modules – Example
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Understanding Module Structure
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
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.
Example of Module Configuration
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
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!
Compiling and Running Modules
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
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.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
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.
Detailed
Creating and Using Modules – Example
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.
Folder Structure
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.java
Example Module Configuration
Below, we outline the configurations for module-info.java for both modules.
- For com.myapp module:
- For
com.utilsmodule:
These files define module dependencies and the packages that are made accessible to other modules, addressing encapsulation effectively.
Compile and Run Instructions
To compile the modules, use the following command:
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:
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.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Folder Structure
Chapter 1 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
src/ ├── com.myapp/ │ ├── module-info.java │ └── com/myapp/Main.java ├── com.utils/ │ ├── module-info.java │ └── com/utils/Utils.java
Detailed Explanation
In this section, we outline the folder structure necessary for developing Java modules. The folder structure shows how two modules are organized: one named 'com.myapp' and another named 'com.utils'. Each module has its own directory containing a 'module-info.java' file, which describes the module's dependencies and the packages it exports. Additionally, each module contains its Java source files. This organization is crucial for maintaining the modular nature of the project.
Examples & Analogies
Think of the folder structure like organizing files in a filing cabinet. Each drawer represents a module, and within the drawer, you have labeled folders (sub-packages and source files) that contain related documents (Java files). This way, everything is neatly organized and easy to find.
Sample module-info.java for com.myapp
Chapter 2 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
module com.myapp {
requires com.utils;
exports com.myapp;
}
Detailed Explanation
This chunk presents a sample 'module-info.java' file for the module 'com.myapp'. The 'requires' statement signifies that this module depends on 'com.utils', meaning it needs functionalities provided by this module to work correctly. The 'exports' statement indicates that the package 'com.myapp' is made accessible to other modules. This explicit declaration is crucial for the modular system to manage dependencies effectively.
Examples & Analogies
Imagine you are part of a team working on a group project. You might need to refer to resources created by another team member (the 'requires' clause). By stating what you need, your team can understand dependencies, and this helps to avoid confusion about what everyone is working on (the 'exports' clause).
Sample module-info.java for com.utils
Chapter 3 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
module com.utils {
exports com.utils;
}
Detailed Explanation
Here, we see the 'module-info.java' file for the 'com.utils' module. This module exports its own package 'com.utils', making it available for other modules to use. Unlike 'com.myapp', this module does not require any other modules. It highlights how a module can stand independently by simply providing functionalities without dependencies.
Examples & Analogies
Consider 'com.utils' as a utilities shop that sells tools (functions) to anyone who needs them. The shop (module) is open for business, and anyone (other modules) can come and take the tools they need. However, it doesn't rely on any other shop to operate.
Compile Command
Chapter 4 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
javac -d out --module-source-path src $(find src -name "*.java")
Detailed Explanation
This command compiles the Java source files in the 'src' directory while maintaining the modular structure. The '-d out' option specifies that the compiled classes should be stored in the 'out' directory. The '--module-source-path src' tells the compiler where to find the module source code, and '$(find src -name "*.java")' identifies all Java files that need to be compiled. This ensures that all modules are correctly compiled and organized.
Examples & Analogies
Think of this command like a chef preparing a meal from multiple ingredients stored in different locations. The chef (compiler) knows where to find the ingredients (Java files) and ensures they are blended together (compiled) into a dish (output classes) ready to be served (executed).
Run Command
Chapter 5 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
java --module-path out -m com.myapp/com.myapp.Main
Detailed Explanation
This command runs the compiled module. The '--module-path out' option indicates where the compiled modules are located, and the '-m' option specifies which module to run, in this case, 'com.myapp', while 'com.myapp.Main' indicates the main class to execute within that module. This command allows the program to be executed using the modularized structure, ensuring that it runs with the correct dependencies and settings.
Examples & Analogies
Imagine you're launching a rocket (your program) that was built using various components (modules). The launch command specifies where the rocket is stored (module path) and which specific rocket to launch (the main class). This precision is essential for a successful launch into space (execution of the program).
Key Concepts
-
Java Module: A self-contained unit of code that groups related packages and resources.
-
module-info.java: The file that describes module dependencies and exports.
-
requires Directive: Indicates dependency on other modules.
-
exports Directive: Controls access to packages by other modules.
-
module-path: Specifies where to find modules in the file system.
Examples & Applications
Example of a module-info.java file for com.myapp that requires com.utils and exports its own package.
The command to compile modules using javac -d out --module-source-path src $(find src -name '*.java').
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
When your code needs to share, exports make it fair, but don't expose all — be aware!
Stories
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!
Memory Tools
R.E.E for Remembering; Requires, Exports, Encapsulated is the way!
Acronyms
M.R.E.S. stands for Module, Requires, Exports, Structure.
Flash Cards
Glossary
- moduleinfo.java
A special Java file used to define a module's name, dependencies, and exported packages.
- requires
A directive in module-info.java that specifies other modules that the current module needs.
- exports
A directive in module-info.java that defines which packages are accessible to other modules.
- modulepath
A path that tells the compiler and the Java runtime where to find the compiled modules.
- module
A self-contained unit of code in Java that encapsulates packages and resources.
Reference links
Supplementary resources to enhance your learning experience.