Modules Flashcards
What types of modules are there?
Named, unnamed, automatic
What are named modules?
They have module-info.java file.
They are on modules path.
What are unnamed modules?
They are on classpath.
They ignore module-info.java.
Modules cannot import them, they don’t export anything.
They can read everything from classpath and other modules.
What are automatic modules?
They don’t have module-info.java file.
They are on modules path.
Name of the module is determined by Java based on the name of the module’s jar file.
All packages are public.
What is typical workflow of defining modules?
Definition
Compiling
Running or packing
How are modules defined?
They are based on file structure. Inside src folder we define a folder and name it something. It can be package. Inside that folder or package we create “module-info.java” file. Everything that is inside that folder is now module.
Do modules need to have main method?
No
Can module-info.java file be empty?
No, it must at least have definition
How do we define module in module-info.java
module full.name.of.the.module {}
How are modules compiled?
javac -d out –module-source-path ./path/to/FirstClass.java ./path/to/SecondClass.java ./path/to/module-info.java
javac -d out -p ./path/to/FirstClass.java ./path/to/SecondClass.java ./path/to/module-info.java
How do we run module?
It must have main class if we want to run it separately.
java –module-path out –module module.name/path.to.Main
java –p out -m module.name/path.to.Main
How do we pack module?
jar -cvf /path/to/module.name.jar -C out/module.name .
What is Jmod?
Command to work with jmod files.
It is made for native libs and for things that don’t go into jars.
What is jlink?
It is a tool that creates smallest possible runnable module, leaving only dependencies module needs to run.
jdeps -summary module.name.jar
jdeps –jdk-internals module.name.jar
jdeps module.name.jar
How do we list available modules?
java -p mods –list-modules
java –list-modules
How do we show module resolution?
Showing resolution means to get information what modules are needed or used with some module. We do it as a part of module startup.
java –show-module-resolution -p path/to/module -m module.name/path.to.Main
How do we describe module with CLI?
java -p path/to/module –describe-module module.name
jar –file path/to/module.jar –describe-module
How can we do migration to modules?
Bottom-up method
Top-down method
Splitting big projects into module
What is Bottom-up method of migration?
Process where:
- main project stays as unnamed module until all other dependencies become module
- dependencies are converted by adding module-info.java
What is Top-down method of migration?
Process where:
- main project is put onto module path and becomes auto. module, and then it is turned into named module
- dependencies are turned into auto. modules and then to named modules
Splitting big projects into module?
We split packages into groups and then we determine relationships
How do we implement services trough modules?
- Defining service interface
- Defining service provider
- Defining service locator
- Defining service consumer
How do we define service interface in modules?
We create module interface with interface methods in certain package.
We define module file in that package, which definition must contain:
“exports full.package.path”
How do we define service provider?
Service provider implements service interface.
We create class in certain package, where we implement service interface defined before.
We create module file in that package, where we must define:
requires full.path.to.interface.module
provides full.path.to.interface.module.InterfaceName with full.path.to.interface.implementation.ClassName
How do we define service locator?
We create class in certain package, where we use ServiceLoader.load() or ServiceLoader.stream():
public void useService() { ServiceLoader<MyService> loader = ServiceLoader.load(MyService.class); for (MyService service : loader) { service.doSomething(); } }
In same package we define module file, where we must define:
requires full.path.to.interface.implementation.ClassName
uses full.path.to.interface.module
How do we define service consumer?
We can create class in some package, where we instantiate service locator and run wanted methods.
In same package we define module file, where we must define:
requires full.path.to.service.locator
What does “requires” keyword mean?
It is used in module-info files to define dependecy that certaing module needs
What does “requires transitive” keywords mean?
If some module B requires another module C, that module C will be required by every module that wants to require B. B can use “requires transitive” to enable every other module to implicitly extend some module.
What does “exports” keyword mean?
It is used to define what package from module will be available to other modules, but only public methods.
What does “exports … to” keyword mean?
It is used to define what package from module will be available to other specific module, but only public methods.
What does “opens” keyword mean?
It is used to define what package methods and variables from module will be available to other modules via reflection.
What does “opens .. to “ keyword mean?
It is used to define what package methods and variables from module will be available to other specific module via reflection.