Chapter 10: Modules Flashcards
What is a module?
A module is a group of one or more packages plus a special file called module-info.java
Given a class in a package ‘zoo.animal.feeding’, create a module-info.java file with no body for that module.
file contents:
module zoo.animal.feeding { }
Where must the file module-info.java be placed?
In the root directory of the module.
Given a module ‘zoo.animal.feeding’ with a file Task.java, in the root directory ‘test’ and a directory ‘mods’ containing custom module files.
Compile the code.
javac -p mods test/zoo/animal/feeding/Task.java test/module-info.java
or
javac –module-path mods test/zoo/animal/feeding/Task.java test/module-info.java
Given a module ‘zoo.animal.feeding’ with a file Task.java in the root directory ‘test’ and a directory ‘mods’ containing custom module files.
Compile the code and place the class files in the directory ‘out’.
javac -p mods -d out test/zoo/animal/feeding/Task.java test/module-info.java
or
javac –module-path mods -d out test/zoo/animal/feeding/Task.java test/module-info.java
Suppose there is a module named book.module which was compiled in the directory ‘test’. Inside that module is a package named com.sybex, which has a class named OCP with a main() method.
Using the above information, run the module.
java –module-path test –module book.module/com.sybex.OCP
–module-path can be replaced by -p and –module can be replaced by -m
Package everything under the ‘test’ directory to the mods directory under the name zoo.animal.feeding
jar -cvf mods/zoo.animal.feeding.jar -C test/ .
Given the following module-info.java file, export the zoo.animal.feeding module so that the module will be available for other modules.
module zoo.animal.feeding { }
module zoo.animal.feeding { exports zoo.animal.feeding; }
Given the module zoo.animal.care, create a module-info.java file that exports the package zoo.animal.care.medical and uses the zoo.animal.feeding module.
module zoo.animal.care {
exports zoo.animal.care.medical;
requires zoo.animal.feeding;
}
Given the module zoo.animal.care, create a module-info.java file that exports the package zoo.animal.care.medical to only the module zoo.staff.
module zoo.animal.care { exports zoo.animal.care.medical to zoo.staff; }
What does the private access control mean with multiple modules?
The private access modifier applied to an element does not allow any outside modules to access the element.
What does the default (package-private) access control mean with multiple modules?
The default access modifier applied to an element does not allow any outside modules to access the element.
What does the protected access control mean with multiple modules?
Accessible to subclasses only if package is exported
What does the public access control mean with multiple modules?
Accessible only if package is exported
What does ‘requires transitive’ do?
While ‘requires’ specifies that the current module depends on another module, ‘requires transitive’ specifies that the current module depends on another module plus all the modules that the other module requires.
Given the modules zoo.animal.feeding, zoo.animal.talks, zoo.animal.care and zoo.staff, does the following code compile?
module zoo.staff { exports zoo.staff; requires zoo.animal.feeding; requires zoo.animal.care; requires zoo.animal.talks; requires transitive zoo.animal.care; }
Java doesn’t allow you to repeat the same module in a requires clause. It is redundant and most like an error in coding.
What does the provides ‘keyword’ do?
The provides keyword specifies that a class provides an implementation of a service. (Think of a service as a fancy interface).
provides zoo.staff.ZooApi with zoo.staff.ZooImpl
What does the provides ‘uses’ do?
The uses keyword specifies that a module is relying on a service.
uses zoo.staff.ZooApi
What does the provides ‘opens’ do?
Allows any module using the module where opens is used to use reflection (inspect and call code at runtime_
What command can we use to find out what the module structure is of a jar file?
java -p {module-path here} -d {module here}
or
java -p {module-path here} –describe-module {module here}
What command can we use to list available modules?
java –list-modules
/
java -p mods –list-modules
What flag can we use to print debugging information when running the modules?
–show-module-resolution
What command can we use to get information about dependencies within a module that are actually used (and not just declared)?
The jdeps command
Use a command to get information about actual dependencies of the module zoo.staff in the directory mods.
jdeps mods/zoo.staff.jar
Use a command to get a summary of the information about actual dependencies of the module zoo.staff in the directory mods.
jdeps -s mods/zoo.staff.jar
or
jdeps -summary mods/zoo.staff.jar
Name all important JMOD operations
create, extract, describe, list, hash