Java SE 9: Module and Module Descriptor Basics (Part-2)

Filed Under: Java

I have already discuss about “Java 9 Module System” in high level in my previous post. I’m going to discuss about two more important concepts in this post: “Module” and “Module Descriptor” basics , which are required to start Java 9 Modules Development.

In this series of “Java Module System” posts, it is my second post. Before reading this post, please read my first post by clicking here “Introduction to Java 9 Module System” to understand about Jigsaw Project details.

Let us dive into this post now.

Post Brief Table of Content:

  • Introduction to Java 9 Module
  • Java 9 Module Basics
  • Java 9 Module Rules
  • Java 9 Module Descriptor
  • What is Module Meta Data?
  • Java 9 Module Descriptor Syntax
  • Are “module”, “requires”, and “exports” Keywords?
  • Java 8 Jars Vs Java 9 Modules
  • ClassPath Vs ModulePath
  • What is Modular JAR?

Introduction to Java 9 Module

A Java 9 Module is a named, self-describing collection of Code and Data. It’s Code is organized as a set of Packages containing types (classes, abstract classes, interfaces, enums etc). It’s Data includes resources and other kinds of static information.


In Simple Terms,

  A Module = Code + Data

Here Even though both Data and Resources are same, in few diagrams I have depicted them in separated boxes for clarity purpose.

NOTE:-
Module Descriptor (“module-info.java”) is a one of the Resources in Java 9 Module.

The two main goals of Java 9 Module System:

  • Reliable Configuration
  • Strong Encapsulation

As we are beginners to the Java 9 Module System, it’s really tough to understand these two terms. No worries, we will discuss these two terms in-depth after developing couple of Modules in my coming posts.

In Java 9 Module System, a Module is a First class citizen.


In Simple Terms, Java has these many First class citizens:

  In OOP (Object Oriented Programming)  = Object 
  In FP (Functional Programming)        = Function or Lambda
  In Module System (Modular Programming)= Module

Java 9 Module Basics

We should remember the following important points about Java 9 Module:

  • Each module has a unique Name.
  • Each module has some description in a source file.
  • A Module description is expressed in a source file called “module-info.java”.
  • As “module-info.java” file describes a Module, it is also known as “Module Descriptor”.
  • A Module Descriptor is a Java file. It is not an XML, Text file or Properties file.
  • By convention, we should use same name “module-info.java” for Module Descriptor.
  • By convention, Module Descriptor file is placed in the top level directory of a Module.
  • Each Module can have any number of Packages and Types.
  • As of now, JDK 9 EA (Early Access) have 95 modules.
  • We can create our own modules.
  • One Module can dependent on any number of modules.
  • Each Module should have one and only one Module Descriptor (“module-info.java”).

Using Java SE 9, we develop Modules that means we can do Modular Programming in Java.

Java 9 Module Rules

We should follow these rules to develop Java 9 Modules:

  • Each module have a unique name
  • As Modules live in a global space in JVM, each module should have a unique name. Like package names and JAR file names, we can use Reverse Domain Name pattern to define a module name.

    For example:-
    We are going to develop modules for www.journaldev.com, then you can use “com.journaldev.mod1” as your first module name, then “com.journaldev.mod2” and so on.

  • Each module should have one and only one Module Descriptor
  • By convention, Java 9 Module system allows “module-info.java” as Module Descriptor. It should be placed at root folder of the Module.

    For example:-
    If We are going to develop “com.journaldev.mod1” module, then we should place our Module Descriptor under “com.journaldev.mod1” name.

  • A Module can have any number of packages.
  • One Module cannot have multiple sub-module code and description.

There are few more import Rules in Java 9 Module System. We should discuss them in my coming posts soon.

Java 9 Module Descriptor

In a Java 9 Module, Module Descriptor is a resource, which contains Module Meta Data. It is NOT an XML or a properties file, a plain Java file.

We must name this file as “module-info.java”. By convention, we mush place it at the root folder of the module. It is used to provide Module Description.

Like other Java source files, a Module file is compiled into “module-info.class” using javac command. We will see it in my next post.

A Module Descriptor is created using “module” keyword as shown below:


module  {
  // Module Meta Data goes here.
}

Example:-


module com.journaldev {
 
}

It is a simple and minimal Module Descriptor example. Now Let us discuss what is a Module Meta Data?

What is Module Meta Data?

As we discussed in the previous section, we can use Module Descriptor to provide Module Meta Data. A Module has the following Meta Data:

  • A unique name.
  • exports clause.
  • requires clause.

Let us discuss these points in-depth with some useful examples here.

  • Module has a unique name.
  • We use “module” keyword to define module name as shown below:
    Example:-

    
    module com.journaldev {
     
    }
    
  • A Module can exports it’s packages to outside world so that other Modules can use them.
  • In Module Descriptor, we use “exports” clause to export a package(s) to outside world or to other Modules.

    Example:-

    
    module com.journaldev.mod1 {
       exports com.journaldev.services;
    }
    

    NOTE:-
    It is not mandatory to export all packages. It’s up-to the Module Owner to decide which one to export and which one not.

  • A Module can import or use other modules packages.
  • In Module Descriptor, we use “requires” clause to import other Module’s packages.

    Example:-

    
    module com.journaldev.mod2 {
       requires com.journaldev.services;
    }
    

    In the previous point, “com.journaldev.mod1” has exported “com.journaldev.services” package so that here “com.journaldev.mod2” is importing and using them.

A Module can have more than this Meta Data. As a Beginner to Java 9 Module System, it is enough to start Modular Programming. We will discuss those few more Meta Data in my coming posts.

We will discuss a Module Descriptor Syntax in the next section.

Java 9 Module Descriptor Syntax

In brief, a Module Descriptor contains 3 main things:

  • A Module name.
  • exports clause.
  • requires clause.

Important points to remember about a Module Descriptor

  • A Module Descriptor just have a Module name and nothing that means no exports clause and no requires clause.
  • A Module Descriptor can have only exports clause, no requires clause. That means it is exports it’s packages to other Modules and NOT depending on any other modules. It’s an Independent module.
  • A Module Descriptor can have both exports clause and requires clause. That means it is exports it’s packages to other Modules and also using other Module’s packages. It is depending on any other modules. It’s NOT an Independent module.
  • A Module Descriptor can have zero, one or more exports clause.
  • A Module Descriptor can have zero, one or more requires clause.

NOTE:- Here “requires” means importing a package from Other module into the current Module.


In Simple Terms,

 "requires" = "imports"

Example:-


module com.hello{
   exports  com.hello;
   requires java.sql;
}

Here “com.hello” Module is exporting it’s package “com.hello” to other Modules and importing or using “java.sql” Java 9 Module.

Are “module”, “requires”, and “exports” Keywords?

As we know in Java 9 Module System, We can use “module”, “requires”, and “exports” keywords to describe a Module Meta Data in a Module Descriptor.

However, all these 3 are not keywords. They are just “Contextual Keywords” only. That means they are keywords only with a Module Descriptor (“module-info.java”) file.

If we used them as Identifiers in the existing code base, Java Compiler or Run-time does NOT throw any error or Exceptions at compile-time and run-time.

We can use them as Identifiers without any issues as shown below:


jshell> int module = 10
module ==> 10

jshell> String requires = "Hello World"
requires ==> "Hello World"

jshell> String exports = "Rams"
exports ==> "Rams"

Java 8 Jars Vs Java 9 Modules

As we know in Java 8 or earlier versions, we use create JARs (Of course WARS or EARs) to deploy them into servers or use them in other applications.

When we create Java 8 or earlier versions project in Eclipse IDE or any IDE, IDE adds lots of JDK JARs into our Project CLASSPATH as shown below:

When we create Java 9 Module project in Eclipse IDE or any IDE, IDE adds lots of JDK Modules into our Project MODULEPATH as shown below:

From Java 9 on-wards, We refer MODULEPATH instead of CLASSPATH. Of course, Java 9 supports both MODULEPATH and CLASSPATH.

We will see about “When we use MODULEPATH?” and “When we use CLASSPATH?” in my coming posts in depth.

In Java 9, we can develop both Public API and Private API in a Module as shown below.

ClassPath Vs ModulePath

As a Java Developer, we know what is CLASSPATH right. So far we are in CLASSPATH Hell or JAR Hell. From Java 9 on-wards, we are going to jump into another Hell: That is MODULEPATH Hell.

A ClassPath is a sequence of classes and packages (or JARs) which are user-defined classes and built-in classes. JVM or Java Compiler requires this to compile the application or classes.

A ModulePath is a sequence of Modules (which are provided in a Folder format or JAR format). If a Module is in folder format, that means its in Exploded Module format. If its in a JAR format, that jar is know as “Modular JAR“.

Don’t worry if you don’t understand these concepts. As we are going to learn new concept, it takes time to understand the new terminology. Move on to the next post.

I will explain all these concepts in-detail in my coming posts. ModulePath is one of the new concept or terminology in Java 9 Module System. We should understand it in-detail.

That’s it all about “Java 9 Module Basics” topic. We will start Java SE 9 Simple Modules Development in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

Comments

  1. Anton says:

    There is a typo at the start, where you tell about first class citizens.
    “… = Moudle” , I think, should be “… = Module”.

    And thank you!

  2. Raal says:

    explained very well!

  3. Vikash says:

    Thank you … Good explanation and written in such a way that any one easily understand the concept.

  4. Julio says:

    Really good post. Help me a lot. I really appreciate your effort.

  5. Subhash says:

    Very useful

  6. santosh says:

    very useful post.

  7. Neel says:

    Hi Rambabu,

    It was really hepfull to build initial application, Thanks for Sharing.

  8. shayan says:

    Thank you very much. You explain it very beautiful and understandable. Keep going my friend

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages