Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.
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.
Listen to a student-teacher conversation explaining the topic in a relatable way.
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.
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
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.java
Below, we outline the configurations for module-info.java
for both modules.
- For com.myapp
module:
com.utils
module: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:
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.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
src/ ├── com.myapp/ │ ├── module-info.java │ └── com/myapp/Main.java ├── com.utils/ │ ├── module-info.java │ └── com/utils/Utils.java
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.
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.
Signup and Enroll to the course for listening the Audio Book
module com.myapp { requires com.utils; exports com.myapp; }
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.
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).
Signup and Enroll to the course for listening the Audio Book
module com.utils { exports com.utils; }
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.
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.
Signup and Enroll to the course for listening the Audio Book
javac -d out --module-source-path src $(find src -name "*.java")
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.
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).
Signup and Enroll to the course for listening the Audio Book
java --module-path out -m com.myapp/com.myapp.Main
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.
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).
Learn essential terms and foundational ideas that form the basis of the topic.
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.
See how the concepts apply in real-world scenarios to understand their practical implications.
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')
.
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
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!
Review key concepts with flashcards.
Review the Definitions for terms.
Term: moduleinfo.java
Definition:
A special Java file used to define a module's name, dependencies, and exported packages.
Term: requires
Definition:
A directive in module-info.java that specifies other modules that the current module needs.
Term: exports
Definition:
A directive in module-info.java that defines which packages are accessible to other modules.
Term: modulepath
Definition:
A path that tells the compiler and the Java runtime where to find the compiled modules.
Term: module
Definition:
A self-contained unit of code in Java that encapsulates packages and resources.