The Java platform module system (JPMS), sometimes known as Java modules, was added to Java 9 as a new feature. The ability to group Java packages and code them into a single entity known as a module has been added. The lack of a module idea in early Java releases makes it challenging to manage all the apps. To address this, Java 9 reorganised JDK into a collection of several modules, allowing developers to only access the specific modules they need for a project and not the entire JDK. Now, in addition to JDK, Java also enables programmers to design modules for building module-based applications. Let’s look into Java roles to identify the difference between packages and modules in Java. We’ll go over what Java modules are and how to use them in this tutorial.
What is Java Model and its Basic?
Developers can package a Java application or an API in independent Java modules using this packaging approach. Developers can also indicate which Java packages contained within a module should be exposed to other Java modules in the form of a modular JAR file. For Java developers, using Java modules has many advantages. The following are some of the key benefits that make using Java modules for application development an excellent choice:
1. Applications are distributed more easily
All Java platform APIs are divided into different modules by JPMS. It enables developers to specify which modules an application needs. Java may package your programme with only the required API Java modules using this information. You would have been required to include all of the APIs in your application without JPMS. The application was frequently needlessly large and challenging to deliver because of the underutilised APIs.
2. Internal packages should be better encapsulated
Which Java packages inside the module can be viewed by users of other Java modules must be explicitly specified by the Java module. The hidden modules can only be utilised internally by the Java module. It creates very well-encapsulated packages.
3. The starting detection of missing classes
Java applications are now bundled in Java modules as of Java 9 and later. Each application module must therefore list the Java API components that it needs. Organizations now verifies the entire module dependency hierarchy to make sure no modules are missing. The JVM reports missing Java modules and shuts down if any necessary modules are not found. Prior to Java 9, missing classes could not be found until the application itself attempted to use them during runtime. If the necessary class cannot be identified by then, the application would crash.
Advantages of Using Java Modules
After describing Java modules and their advantages, let’s look at some of the fundamentals of Java modules.
1. The number of packets
One or more Java packages that are part of the same application are frequently included in Java modules. A module could be a Java Platform or an entire Java programme.
2. Naming of Java components
A Java module needs a special name. It uses the same name rules as Java packages, except underscores are not allowed because Java plans to use them as reserved identifiers in the future. This includes in packages and classes as well.
3. Module descriptor
A Java module descriptor called module-info.java is necessary for Java modules. The module root directory is where it needs to be. The module descriptor lists the packages that must be exported as well as the additional packages that the module needs.
4. Exports of modules
Packages that are necessary for the other modules must be specifically indicated, as was previously stated. The module descriptor contains a declaration of the exported packages.
What Is the Difference Between a Package and a Module in Java?
Java 9 introduces Java modules through the Java Platform Module System (JPMS). Depending on where you read it, the Java Platform Module System is also sometimes referred to as Java Jigsaw or Project Jigsaw. Jigsaw was the project name used internally throughout development. Later, Jigsaw adopted the name. A group of related classes and interfaces are grouped under the namespace of a package.
Packages
Let’s say we have two classes, Employee
and Department
, which are related to each other. We can put them in a package named company
as follows:
We can then use these classes in another class by importing the company
package:
// Employee.java
package company;
public class Employee {
// fields and methods
}
// Department.java
package company;
public class Department {
// fields and methods
}
Modules
With modules, we can organize the company
package and its dependencies into a self-contained unit. Let’s say we have a module named company.module
that contains the company
package and its dependencies:
javaCopy code
// module-info.java
module company.module {
exports company;
}
The exports
statement indicates that the company
package is part of the module and is exposed to other modules.
We can then use this module in another module by declaring a dependency on it:
// module-info.java
module my.module {
requires company.module;
}
// Main.java
import company.Employee;
import company.Department;
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
Department department = new Department();
// use employee and department objects
}
}
Here, the requires
statement indicates that the my.module
module depends on the company.module
module, and can use the company
package and its classes.
Note that we don’t need to import the company
package explicitly because it is already part of the company.module
module, which is a dependency of my.module
.
List of 24 Modules in Java
- java.base: contains the fundamental classes and interfaces that are used by all other modules.
- java.compiler: contains the Java programming language compiler, which is used to compile Java source code into bytecode.
- java.datatransfer: provides a framework for transferring data between different parts of an application or between different applications.
- java.desktop: contains the classes and interfaces for creating graphical user interfaces (GUIs) in desktop applications.
- java.instrument: provides a mechanism for instrumenting Java bytecode at runtime.
- java.logging: provides a logging framework for Java applications.
- java.management: provides the Java Management Extensions (JMX) API for managing and monitoring Java applications.
- java.management.rmi: provides the RMI connector for the JMX API, which allows remote management and monitoring of Java applications.
- java.naming: provides the Java Naming and Directory Interface (JNDI) API for accessing naming and directory services.
- java.net.http: provides the HTTP client and WebSocket APIs for making HTTP requests and handling server responses.
- java.prefs: provides a way to store and retrieve user and system preference data.
- java.rmi: provides the Remote Method Invocation (RMI) API for remote communication between Java applications.
- java.scripting: provides a framework for integrating scripting languages into Java applications.
- java.se: includes all the modules in the Java SE platform.
- java.security.jgss: provides the Java Generic Security Services (JGSS) API for implementing security mechanisms such as Kerberos and GSS-API.
- java.security.sasl: provides the Simple Authentication and Security Layer (SASL) API for implementing authentication mechanisms.
- java.smartcardio: provides the Java Smart Card I/O API for accessing smart cards and other smart card devices.
- java.sql: provides the JDBC API for accessing relational databases.
- java.sql.rowset: provides the JDBC RowSet API for working with disconnected data.
- java.transaction.xa: provides the Java Transaction API (JTA) for coordinating distributed transactions.
- java.xml: provides the Java API for XML Processing (JAXP) for parsing and manipulating XML documents.
- java.xml.crypto: provides the Java API for XML Digital Signatures (XMLDSig) and XML Encryption (XMLEnc) for securing XML documents.
- jdk.crypto.cryptoki: provides the Java Cryptography Architecture (JCA) API for integrating with hardware security modules.
- jdk.zipfs: provides the Zip file system provider for working with ZIP files.
Basic Outline
Packages can be conceptualised as being analogous to various computer directories. You may keep photos in one folder, scripts or programmes in another, and HTML pages in a third. It makes sense to keep things organised by grouping comparable classes and interfaces into packages because software created using the Java programming language might be made up of hundreds or thousands of different classes. A module is a grouping of related Java packages and resources with a descriptor file that contains information about the packages and resources this module exposes, the packages it uses currently, and other details. You can package a Java application or Java API as a distinct Java module using the packaging process known as a Java module.
A modular JAR file is how a Java module is packaged. A Java module has the option to declare which Java packages should be available to other Java modules that use it. A Java module must also list the Java modules it needs in order to function.
Package
- A package puts together relevant classes but cannot be deployed by itself.
- The first version of Java includes packages.
- Packages were created to allow developers to have classes with the same name in separate packages and to keep similar classes together.
- Even though they are private, classes declared within a package are available via reflection.
- A package descriptor is not necessary for packages.
Module
- A module gathers together similar packages and can be deployed on its own.
- By Java 9, modules were added.
- Security concerns and a desire to make the JDK smaller led to the addition of modules.
- Classes declared inside a module cannot be accessed by reflection from outside the module.
- A module is made up of numerous packages (folders containing.java files). A module is essentially a library with a particular purpose.
There are 24 modules in Java.
The latest version of Java was Java 17, which was released on September 14, 2021.