Chapter 12: Modules Flashcards
What is the main purpose of the Java Platform Module System (JPMS)?
JPMS groups related packages into modules, allowing controlled access and improved code organization.
How does JPMS compare to JAR files?
Like a JAR file, but with explicit control over which packages are accessible outside the module.
What are the three key components of JPMS?
Module JAR format, JDK partitioning into modules, and additional Java tool command-line options.
What is a module in Java?
A group of one or more packages plus a module-info.java file that declares module properties.
Why were Java modules introduced? 5 items
To improve access control, dependency management, security, performance, and to support custom Java builds.
How do modules provide better access control?
They allow packages to be accessible only within the module, preventing unintended access.
How do modules improve dependency management?
They declare dependencies, enabling Java to detect missing JARs at startup instead of runtime.
What is a key advantage of using custom Java builds?
They include only necessary JDK components, reducing runtime size and improving efficiency.
How do modules enhance security?
By omitting unused JDK parts, reducing exposure to vulnerabilities.
How do modules improve performance?
Smaller runtime size leads to faster startup and lower memory usage.
What is unique package enforcement in Java modules?
Ensures each package comes from only one module, preventing conflicts and confusion.
How do Java modules enforce strong encapsulation?
Packages are not accessible by default; only explicitly exported packages can be used by other modules.
What keyword is used to expose a package in Java modules?
The exports keyword, e.g., exports com.example.api;.
How can a module restrict access to its exported packages?
By using exports … to, e.g., exports com.example.api to com.example.moduleB; to limit access to a specific module.
How can a module allow reflective access to a package?
Using the opens keyword, e.g., opens com.example.internal to some.framework.module;.
What does the requires keyword do in a Java module?
It specifies dependencies on other modules, ensuring only required modules are accessible.
What does the –module-path option do in Java’s module system?
The –module-path option specifies the locations of required modules. It allows the Java runtime to locate and load modules that are needed for execution. Multiple module paths can be separated using ; on Windows or : on macOS/Linux.
How do you execute a specific class from a named module using the –module option?
The –module option is used to specify a module and the class within it that should be executed.
What is a named module in Java?
A named module is a module that has an explicit name defined in a module-info.java file. It is part of Java’s module system and follows strict encapsulation rules. Named modules explicitly declare their dependencies and exported packages.
How is a named module different from an unnamed module?
A named module has a module-info.java file and follows strict module rules, whereas an unnamed module does not have a module descriptor and can access all other modules. Named modules provide better encapsulation and dependency management.
What is the purpose of the jdeps command in Java?
jdeps analyzes the dependencies of Java class files and shows the packages or classes that are required by a given class or JAR file.
How do you use jdeps to analyze a JAR file and display package-level dependencies?
You run jdeps myapp.jar, and it will show which packages the classes in myapp.jar depend on.
Which jdeps option shows class-level dependencies instead of package-level?
The -verbose:class option displays class-level dependencies: jdeps -verbose:class myapp.jar.
What does the
--module-path a.jar:b.jaroption do in a jdeps command?
It tells jdeps where to find required modules, treating both a.jar and b.jar as modular JARs. This enables jdeps to resolve dependencies correctly.
What is the purpose of
--module com.example.ain jdeps?
It specifies the module to analyze, using the name defined in module-info.java (in this case, com.example.a).
Why is jdeps –module-path … –module … preferred over –class-path for modular JARs?
Because –module-path respects JPMS (Java Platform Module System) and understands named and automatic modules, while –class-path treats JARs as legacy classpath entries.
Why does a Java modular project require a folder structure like src/com.example/com/example/Main.java?
In a Java modular project using the –module-source-path option, the compiler expects the source root (e.g., src/) to contain one folder per module, where the folder name matches the module name declared in module-info.java. Inside that folder, the standard Java package structure begins. So for a module named com.example, the correct structure is src/com.example/com/example/Main.java for a class in the com.example package.
What does the –module-source-path option tell the Java compiler about source structure?
The –module-source-path option tells the Java compiler that each immediate subdirectory of the given source root (e.g., src/) is a distinct module. The name of each folder must match the module declaration in its module-info.java, and inside that folder, source files must follow the standard package-based directory structure (e.g., com/example for package com.example;).
What happens if the folder structure in a modular Java project does not separate the module folder from the package folders?
If the source files in a modular Java project are placed directly under the package structure (e.g., src/com/example) instead of under a module-named folder (e.g., src/com.example/com/example), the compiler will not recognize the files as belonging to a named module. This results in errors like “module not found on module source path,” because the expected module-info.java and the corresponding package files aren’t located under a properly named module directory.