Creating and Using Modules – Example - 13.7 | 13. Java Modules and the JPMS (Java Platform Module System) | Advance Programming In Java
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Understanding Module Structure

Unlock Audio Lesson

0:00
Teacher
Teacher

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

Student 1
Student 1

I think it has some files that describe the module?

Teacher
Teacher

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?

Student 2
Student 2

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

Teacher
Teacher

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

0:00
Teacher
Teacher

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?

Student 3
Student 3

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

Teacher
Teacher

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?

Student 4
Student 4

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

Teacher
Teacher

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`?

Student 2
Student 2

It exports the `com.utils` packages!

Teacher
Teacher

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

Teacher
Teacher

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

0:00
Teacher
Teacher

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?

Student 1
Student 1

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

Teacher
Teacher

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?

Student 3
Student 3

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

Teacher
Teacher

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

Student 4
Student 4

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

Teacher
Teacher

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

Teacher
Teacher

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 a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

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

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:

Code Editor - java
  • For com.utils module:
Code Editor - java

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:

Code Editor - bash

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:

Code Editor - bash

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

Java 9 || Session - 61 || JPMS (Java Platform Module System ) Part - 17 by Durga sir
Java 9 || Session - 61 || JPMS (Java Platform Module System ) Part - 17 by Durga sir
Overview of the Java Memory Model
Overview of the Java Memory Model

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Folder Structure

Unlock Audio Book

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

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 Audio Book

Signup and Enroll to the course for listening the Audio Book

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 Audio Book

Signup and Enroll to the course for listening the Audio Book

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 Audio Book

Signup and Enroll to the course for listening the Audio Book

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 Audio Book

Signup and Enroll to the course for listening the Audio Book

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

Definitions & Key Concepts

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.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • 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

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎵 Rhymes Time

  • When your code needs to share, exports make it fair, but don't expose all — be aware!

📖 Fascinating 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!

🧠 Other Memory Gems

  • R.E.E for Remembering; Requires, Exports, Encapsulated is the way!

🎯 Super Acronyms

M.R.E.S. stands for Module, Requires, Exports, Structure.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

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.