
上QQ阅读APP看书,第一时间看更新
How to do it...
- Compile the code and place the compiled classes in a directory, say, mods:
javac -d mods --module-source-path . $(find . -name *.java)
- Build a modular JAR for the math.util module:
jar --create --file=mlib/math.util@1.0.jar --module-version 1.0
-C mods/math.util .
Do not forget the dot ( .) at the end of the preceding code.
- Build a modular JAR for the calculator module, specifying the main class to make the JAR executable:
jar --create --file=mlib/calculator@1.0.jar --module-version 1.0
--main-class com.packt.calculator.Calculator -C mods/calculator .
The critical piece in the preceding command is the --main-class option. This enables us to execute the JAR without providing the main class information during execution.
- Now, we have two JARs in the mlib directory: math.util@1.0.jar and calculator@1.0.jar. These JARs are called modular JARs. If you want to run the example, you can use the following command:
java -p mlib -m calculator
- A new command-line option for the JAR command has been introduced in Java 9, called -d , or --describe-module. This prints the information about the module that the modular JAR contains:
jar -d --file=mlib/calculator@1.0.jar
The output of jar -d for calculator@1.0.jar is as follows:
calculator@1.0 requires mandated java.base requires math.util conceals com.packt.calculator main-class com.packt.calculator.Calculator
jar -d --file=mlib/math.util@1.0.jar
The output of jar -d for math.util@1.0.jar is as follows:
math.util@1.0 requires mandated java.base exports com.packt.math
We have provided the following scripts to try out the recipe code on Windows:
- compile-math.bat
- compile-calculator.bat
- jar-math.bat
- jar-calculator.bat
- run.bat
We have provided the following scripts to try out the recipe code on Linux:
- compile.sh
- jar-math.sh
- jar-calculator.sh
- run.sh
You have to run the scripts in the order they have been listed.