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