Cheatsheet-Day12-Lesson7-Java Modules
Cheatsheet-Day12-Lesson7-Java Modules
Before Java 9, the JDK was a monolithic set of packages, packaged in one jar file,
the rt.jar, which any java application would need to deploy alongwith it's own
code.
rt.jar:
rt.jar 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 java.base ....
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.
java --list-modules
java --describe-module java.logging
-m: module
module-info.java
module myfirstmodule{
module com.lti.user{
In an application, there can be several modules, and each module contains precisely
one module-info.java
myfirstmodule
--src
--com.lti.user
---User.java
---Application.java
--com.lti.admin
--Admin.java
--module-info.java
mysecondmodule
--src
--com.cit.user
---Test.java
---App.java
--com.citi.admin
--Admin.java
--module-info.java
We can also package the above compilation output in a JAR(modular jar) file.
myfirstmodule.jar
--META-INF
--MANIFEST.MF
--com.lti.user
---User.class
---Application.class
--com.lti.admin
--Admin.class
--module-info.class
firstmodule
--src
--com.lti.greet
--Hello.java
--module-info.java
Non-modular java applications run on "classpath" but modular java applications run
on "modulepath"
bin
--firstmodule
--com.lti.greet
--Hello.class
--module-info.class
----------------------------------------------
Module created in project "Global":
-------------------------------------------------------------------------------
org.module.base--------(required)---------->org.module.util-----------(required
transitive)---->org.module.global
export com.lti.util
exports com.lti.app
exports
com.lti.demo
Module created in project "Util":
module name: org.module.util
--src
--com.lti.util/MyUtilities.java
--module-info.java{
require transitive org.module.global
exports com.lti.util
}
1. Direct cycle:
org.module.util----------->org.module.global
org.module.util<-----------org.module.global
2. Indirect cycle
org.module.util----------->org.module.global
org.module.base<-----------org.module.global
org.module.base------------>org.module.util
--------------------------------------------Services in
Modules-----------------------------------------------------------------
-------------------------------------------------
Quiz-------------------------------------------------------------------------------
Question 1:
Your application is packaged in myapp.jar and depends on a jar named datalayer.jar,
which in turn depends on mysql-connector-java-8.0.11.jar. The following packages
exist in these jars:
myapp.jar: com.abc.myapp
datalayer.jar: com.abc.datalayer
mysql-connector-java-8.0.11.jar: com.mysql.jdbc
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 abc.myapp{
requires com.abc.datalayer;
}
B. module abc.myapp{
exports com.abc.myapp;
}
C. module abc.myapp{
requires datalayer;
}
D.module abc.myapp{
requires datalayer;
requires mysql-connector-java;
}
E. module abc.myapp{
requires datalayer;
requires mysql-connector-java-0.0.11;
}
F. module abc.myapp{
requires com.abc.datalayer;
requires com.mysql.jdbc;
}
Question 2:
B. Main goals of the module system are to improve security with strong
encapsulation and stability with reliable dependencies.
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 module.java:
module autos{
}
B.
//In file autos.java:
module autos{
}
C.
//In file module-info.java:
module autos{
}
D.
//In file module-info.java:
module cars{
exports com.car;
}
module trucks{
requires cars;
}
E.
//In file module-info.java:
module-info autos{
}
Question 6:
You are creating an acme.accounting module that depends on acme.math module and
makes its com.acme.accounting package available to all other modules. Which of the
following files correctly defines this module?
Question 8:
Given:
module broker{
exports org.broker.api;
}
The broker module contains org.broker.api.Installer interface, which is implemented
by com.foo.AppInstaller class of com.foo module.
Which of the following is a valid module definition for com.foo module?
A. module com.foo{
exports broker;
}
B. module com.foo{
provider broker;
}
module com.foo{
requires broker;
provides org.broker.api;
}
C. module com.foo{
requires broker;
exports com.foo;
provides org.broker.api.Installer with com.foo.AppInstaller;
}
D. module com.foo{
exports com.foo;
provides org.broker.api.Installer with com.foo.AppInstaller;
}
Question 9:
module enthu.finance{
exports com.enthu.Reports;
requires enthu.utils;
}
Select correct statements.