Creating, Compiling and Running Modules
1) IntelliJ, 2) Terminal
Create a new project in IntelliJ called
ModuleTesting
Right click on the project name,
Right Click --> New ----> Module
Click on Next
MyFirstModule
Accept all the default and click finish.
MyFirstModule
D:\ModuleTesting\MyFirstModule
D:\ModuleTesting\MyFirstModule
The src folder is a path that is available in our folder.
Right click on the src folder and select
âOpen in Terminalâ
This is the path
D:\ModuleTesting\MyFirstModule\src>
Exit out of the terminal.
Make sure that you click on the src folder that is below MyFirstModule
Even though we have run the code, we are not yet running the code as a module.
C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath
This is a clue that we are not running the code as a module.
1 more step that we need to take to get this to work as a module.
Right click on the src folder,
new --> module-info.java
We will be discussing module-info.java in a later lesson. Note that the module name, follows the module keyword,
and is the name of the module that we created earlier in this video.
The module body is required
module MyFirstModule {
}
After you have created the module, you will no longer see the -classpath option after you compile the program.
--module -path
-m
The point is, we are now executing our code as a module.
Open up a terminal in IntelliJ and execute the code from the command line.
We want to manually execute the code from the command line, using different options.
out/production
directory is the directory created by IntelliJ
A jar file requires module-info in the root folder.
-m is the alias for module.
This is the first command that I executed from the command line.
java --module-path out\production\ -m MyFirstModule
This command
D:\ModuleTesting-->java --module-path out\production\ -m MyFirstModule/modular.HelloWorld
did not work until I rebuilt the project.
The following command generated a jar file.
jar --create --file MyFirstModule.jar --main-class modular.HelloWorld -C out\production\MyFirstModule\ .
D:\ModuleTesting-->jar -f MyFirstModule.jar --list
META-INF/
META-INF/MANIFEST.MF
module-info.class
modular/
modular/HelloWorld.class
jar -f MyFirstModule.jar -d
MyFirstModule jar:file:///D:/ModuleTesting/MyFirstModule.jar/!module-info.class
requires java.base mandated
contains modular
main-class modular.HelloWorld
java --module-path . --describe-module MyFirstModule
MyFirstModule file:///D:/ModuleTesting/./MyFirstModule.jar
requires java.base mandated
contains modular
Specify the path using
âmodule -path
java -p . -d MyFirstModule
D:\ModuleTesting-->java -p . -d MyFirstModule
MyFirstModule file:///D:/ModuleTesting/./MyFirstModule.jar
requires java.base mandated
contains modular
Question: How do we execute the code in the Jar File?
We tell the JVM to look for a jar file or exploded module directory using the following.
java --module-path . --module MyFirstModule
-----
D:\ModuleTesting-->java --module-path . --module MyFirstModule
Hello new modular world
D:\ModuleTesting>jdeps MyFirstModule.jar
MyFirstModule
[file:///D:/ModuleTesting/MyFirstModule.jar]
requires mandated java.base (@11.0.8)
MyFirstModule -> java.base
modular -> java.io java.base
modular -> java.lang java.base
D:\ModuleTesting>