Java Platform Module System( Java Modules)
Java modules was introduced in Java 9.
Module is all about structural changes in java application.
Before Java 9, the JDK was a monolithic set of packages, packaged in one jar file,
the [Link], which any java application would need to deploy alongwith it's own
code.
[Link]:
[Link] contains all of the compiled class files for the base Java Runtime
environment, as well as the bootstrap classes, which are the run time classes that
comprise the Java platform core API.
Module is a group of packages. But unlike jars, modules add another level of
encapsulation where all the packages may not be exposed to outside environment.
Also, an application can mention on what all modules they require for their
application.
Standard modules:
All of the modules in the Java SE are standard modules as defined by the JCP.
A standard module, may contain both standard and non-non standard API packages.
A standard package is prefixed with "java." or "javax.", like [Link] ....
Non-standard modules:
The JDK: jdk APIs are specific to the JDK.
These APIs are in modules whose names start with a "jdk." prefix.
A non-standard module must not export any standard API packages.
examining modules from the command line:
java --list-modules
java --describe-module [Link]
jDeps: Java Class Dependency Analyzer
-m: module
jdeps --print-module-deps -m [Link]
jdeps --print-module-deps --module [Link]
jdeps --list-deps -m [Link]
jdeps --list-reduced-deps -m [Link]
Creating a Module in Java:
[Link]
module myfirstmodule{
module [Link]{
In an application, there can be several modules, and each module contains precisely
one [Link]
myfirstmodule
--src
--[Link]
---[Link]
---[Link]
--[Link]
--[Link]
--[Link]
mysecondmodule
--src
--[Link]
---[Link]
---[Link]
--[Link]
--[Link]
--[Link]
We can also package the above compilation output in a JAR(modular jar) file.
[Link]
--META-INF
--[Link]
--[Link]
---[Link]
---[Link]
--[Link]
--[Link]
--[Link]
firstmodule
--src
--[Link]
--[Link]
--[Link]
Non-modular java application:
C:\Program Files\jdk-16.0.2\bin\[Link]
-[Link]=Cp1252
-classpath "D:\OCP-NEW BATCH\OCP Demos\FirstModule\bin"
-XX:+ShowCodeDetailsInExceptionMessages [Link]
Non-modular java applications run on "classpath" but modular java applications run
on "modulepath"
Module java application:
C:\Program Files\jdk-16.0.2\bin\[Link]
-[Link]=Cp1252
-p "D:\OCP-NEW BATCH\OCP Demos\TestModule\bin"
-XX:+ShowCodeDetailsInExceptionMessages
-m testmodule/[Link]
-p: module path
-m: module name
-d: directory in which you want to place your class files. By default eclipse use
bin folder. IntelliJ, uses out folder.
javac -d bin src/com/lti/greet/[Link] src/[Link]
bin
--firstmodule
--[Link]
--[Link]
--[Link]
java -p bin -m firstmodule/[Link]
java -p bin --describe-module firstmodule
Create a modular jar:
jar --create --file [Link] --main-class [Link] -C bin/ .
running a jar file:
java -jar [Link]
jdeps --module-path bin [Link]
----------------------------------------------
Module created in project "Global":
module name: [Link]
--src
--[Link]/[Link]
--[Link]/[Link]
--[Link]{
exports [Link];
exports [Link];
}
Module created in project "Client":
module name: [Link]
--src
--[Link]/[Link]
--[Link]{
require [Link]
}
java -p bin --describe-module [Link]
-------------------------------------------------------------------------------
[Link]--------(required)---------->[Link]-----------(required
transitive)---->[Link]
export [Link]
exports [Link]
exports
[Link]
Module created in project "Util":
module name: [Link]
--src
--[Link]/[Link]
--[Link]{
require transitive [Link]
exports [Link]
}
Module created in project "Base":
module name: [Link]
--src
--[Link]/[Link]
--[Link]{
require [Link]
}
jdeps --module-path bin -m [Link]
jdeps --module-path bin;"D:\OCP-NEW BATCH\OCP Demos\Global\bin" -m [Link]
jdeps --module-path bin;"D:\OCP-NEW BATCH\OCP Demos\Util\bin";"D:\OCP-NEW BATCH\OCP
Demos\Global\bin" -m [Link]
Friendly module: Qualified exports
exports [Link] to [Link],[Link];
Cyclic dependency is not allowed.
1. Direct cycle:
[Link]----------->[Link]
[Link]<-----------[Link]
2. Indirect cycle
[Link]----------->[Link]
[Link]<-----------[Link]
[Link]------------>[Link]
--------------------------------------------Services in
Modules-----------------------------------------------------------------
Project Name: Service
module1: [Link]
[Link]/ServiceRegistry(interface)-->greetings()
[Link]{
exports [Link];
}
Project Name: ProviderOne
module2: [Link]
[Link].impl1/Serviceimpl1(class) which implements the ServiceRegistry
interface---->override the greetings method
[Link]{
require [Link];
provides [Link]
with [Link].impl1.Serviceimpl1
}
Project Name: ProviderTwo
module3: [Link]
[Link].impl2/Serviceimpl2(class) which implements the ServiceRegistry
interface---->override the greetings method
[Link]{
require [Link];
provides [Link]
with [Link].impl2.Serviceimpl2
}
Project Name: Consumer
module4: [Link]
[Link]{
require [Link];
uses [Link]
}
opens [Link]; //reflexive access
opens [Link] to [Link];
-------------------------------------------------
Quiz-------------------------------------------------------------------------------
Question 1:
Your application is packaged in [Link] and depends on a jar named [Link],
which in turn depends on [Link]. The following packages
exist in these jars:
[Link]: [Link]
[Link]: [Link]
[Link]: [Link]
You have decided to modularize your application even though datalayer and mysql
libraries are still not modularlized. Which of the following would be a valid
module-info for your app?
A. module [Link]{
requires [Link];
}
B. module [Link]{
exports [Link];
}
C. module [Link]{
requires datalayer;
}
[Link] [Link]{
requires datalayer;
requires mysql-connector-java;
}
E. module [Link]{
requires datalayer;
requires mysql-connector-java-0.0.11;
}
F. module [Link]{
requires [Link];
requires [Link];
}
Question 2:
Identify correct statements about the module system of Java.
A. Only an application structured as modular can be run on a modular JDK.
B. Main goals of the module system are to improve security with strong
encapsulation and stability with reliable dependencies.
C. Code in modules and traditional JARs on the classpath cannot coexist in an
application.
D. Modules have concealed packages for internal use and exported packages for
shared code with other modules.
Question 4:
Which of the following are valid module definitions?
A.
//In file [Link]:
module autos{
}
B.
//In file [Link]:
module autos{
}
C.
//In file [Link]:
module autos{
}
D.
//In file [Link]:
module cars{
exports [Link];
}
module trucks{
requires cars;
}
E.
//In file [Link]:
module-info autos{
}
Question 6:
You are creating an [Link] module that depends on [Link] module and
makes its [Link] package available to all other modules. Which of the
following files correctly defines this module?
A. //In file [Link]:
module [Link]{
requires [Link];
exports [Link];
}
B. //In file [Link]:
module [Link]{
requires [Link].*;
exports [Link].*;
}
C. //In file [Link]:
module [Link]{
requires [Link];
exports [Link].*;
}
D. //In file [Link]:
module-info [Link]{
requires [Link];
exports [Link];
}
E. //In file [Link]:
module [Link]{
requires [Link];
exports [Link];
}
Question 8:
Given:
module broker{
exports [Link];
}
The broker module contains [Link] interface, which is implemented
by [Link] class of [Link] module.
Which of the following is a valid module definition for [Link] module?
A. module [Link]{
exports broker;
}
B. module [Link]{
provider broker;
}
module [Link]{
requires broker;
provides [Link];
}
C. module [Link]{
requires broker;
exports [Link];
provides [Link] with [Link];
}
D. module [Link]{
exports [Link];
provides [Link] with [Link];
}
Question 9:
Given the following contents of [Link],
module [Link]{
exports [Link];
requires [Link];
}
Select correct statements.
A. Module name is finance.
B. [Link] is the name of the class that this module exports.
C. This module depends on [Link] package.
D. This file must be present in [Link] directory if the source code is to be
compiled using the --module-source-path option.
E. Other modules that depend on this module will be able to access
[Link] package as well as [Link] package.
MyApp
|
|-[Link]
|
|--[Link]
| src
| --[Link]/[Link]
| --[Link]/[Link]
| --[Link]
|
|-- [Link]
src
--[Link]/[Link]
--[Link]/[Link]
--[Link]
-m myfirstmodule ---------> --moudule-source-path [Link]