AllRounder.ai

Enrol to start learning

Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.

Enrol free

13.7. Creating and Using Modules – Example

Interactive Audio Lesson

Session 1: Understanding Module Structure

Unlock the classroom podcast

The transcript is above and free to read. A free account plays the conversation back.

Create a free account
Sarah
SarahInstructor

Today, we'll start by discussing the basic structure of a Java module. Does anyone know what a Java module usually includes?

Noah
Noah

I think it has some files that describe the module?

Sarah
SarahInstructor

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?

Isabella
Isabella

It should have 'requires' and 'exports' directives!

Sarah
SarahInstructor

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.

Session 2: Example of Module Configuration

Unlock the classroom podcast

The transcript is above and free to read. A free account plays the conversation back.

Create a free account
Robert
RobertInstructor

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?

Akash
Akash

Because com.myapp needs to use features from com.utils!

Robert
RobertInstructor

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?

Ananya
Ananya

It allows other modules to access the packages inside com.myapp.

Robert
RobertInstructor

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?

Isabella
Isabella

It exports the com.utils packages!

Robert
RobertInstructor

Correct! This two-way interaction through module definitions is essential for reusable software components. Let's summarize what we learned.

Robert
RobertInstructor

Today we discussed the importance of module configurations and how dependencies and exports facilitate code structure. Remember the importance of encapsulation!

Session 3: Compiling and Running Modules

Unlock the classroom podcast

The transcript is above and free to read. A free account plays the conversation back.

Create a free account
Sarah
SarahInstructor

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?

Noah
Noah

I think it's something like javac -d out --module-source-path src. Is that right?

Sarah
SarahInstructor

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?

Akash
Akash

It locates all Java files in the specified source path, right?

Sarah
SarahInstructor

Exactly! Now, after compiling, how do we run the application?

Ananya
Ananya

We use java --module-path out -m com.myapp/com.myapp.Main!

Sarah
SarahInstructor

Exactly correct! This indicates the module and the entry point. Let's wrap up today's lessons.

Sarah
SarahInstructor

In summary, we covered the compile command and how to run our Java modules effectively. Understanding these commands is key to working with JPMS.

Overview

Short Summary

This section demonstrates how to create and utilize Java modules, detailing the folder structure and example module configurations.

Medium Summary

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 Summary

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:

- python
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:
- java
module com.myapp {
    requires com.utils;
    exports com.myapp;
}
  • For com.utils module:
- java
module com.utils {
    exports com.utils;
}

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:

- bash
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:

- bash
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.

Reference YouTube Videos

Audio Book

Voice:
Folder Structure

Unlock the audio lesson

The script is above and free to read. A free account plays it back, in the voice you pick.

Create a free account
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

Unlock the audio lesson

The script is above and free to read. A free account plays it back, in the voice you pick.

Create a free account
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

Unlock the audio lesson

The script is above and free to read. A free account plays it back, in the voice you pick.

Create a free account
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

Unlock the audio lesson

The script is above and free to read. A free account plays it back, in the voice you pick.

Create a free account
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

Unlock the audio lesson

The script is above and free to read. A free account plays it back, in the voice you pick.

Create a free account
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

Core takeaways and short definitions to help you quickly recall the key ideas from this section.

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

Step-by-step examples to apply the section's ideas and test your understanding.

1

Example of a module-info.java file for com.myapp that requires com.utils and exports its own package.

2

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.