This is an example project that shows how to compile and run an unnamed module. In essence, we migrate the project without transforming the codebase to a module.
.
├── README.adoc
├── arguments.args
├── jar (1)
│ └── modi.jar
├── out (2)
│ └── production
│ └── modi
│ └── com
│ └── example
│ └── modi
│ ├── entrypoint
│ │ └── Main.class
│ └── query
│ └── Modi.class
└── src
└── com
└── example
└── modi
├── entrypoint
│ └── Main.java
└── query
└── Modi.java
-
If you follow the steps over the section 4, you should see the related JAR file on the
jardirectory. -
After compiling the project using the commands below, the
outdirectory will be created along with the compiled sources.
The Java compiler tool first compiles the Main and the Modi files and places their compiled classes into the out/production/modi directory.
src/com/example/modi/entrypoint/Main.java src/com/example/modi/query/Modi.java -d out/production/modi
$ javac --version javac 9.0.1 $ javac @arguments.args
This is the regular exploded directory containing the application class files. The exploded directory can refer to the unnamed module. You can run the project using two approaches:
$ java --version java 9.0.1 Java(TM) SE Runtime Environment (build 9.0.1+11) Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode) $ cd out/production/modi $ java com.example.modi.entrypoint.Main java.base :) $ #or $ java -cp out/production/modi com.example.modi.entrypoint.Main java.base (1) :)
-
-cpis the abbreviation for--classpathand--class-path.
We can generate a corresponding JAR file with the following command:
$ jar -c \ (1) -f jar/modi.jar \ (2) -e com.example.modi.entrypoint.Main \ (3) -C out/production/modi/ . (4)
-
A new archive file we want to create. Either
--createor-coperation flags can be used. -
The new archive file name will be
modi.jarthe--fileflag is the equivalent of the-fflag. -
We specify the entry point (main-class). we can do so the thing with the
--main-class com.example.modi.entrypoint.Mainflag as well. -
We now change the directory to
out/production/modiand look at the all compiled class files in order to put them in a JAR file. You should see themodi.jarfile in thejardirectory after running the command. Note that, thejardirectory must exists.
The directory tree below shows the generated JAR structure. It’s similar to the explode unnamed module.
modi.jar
├── META-INF
│ └── MANIFEST.MF
└── com
└── example
└── modi
├── entrypoint
│ └── Main.class
└── query
└── Modi.class
We are able to inspect the module description of our JAR file. We can find the answers of how many platform modules are required, how many packages the JAR contains and what the main class is.
$ jar -d \ (1) -f jar/modi.jar No module descriptor found. Derived automatic module. (2) modi automatic requires java.base mandated (3) contains com.example.modi.entrypoint (4) contains com.example.modi.query main-class com.example.modi.entrypoint.Main (5)
-
-dis the sort-form of the--describe-moduleflag. This is the main operation flag so we cannot use both-cand-dat the same time. -
Since the project is not modularized yet as It doesn’t contain a module description file, the name
modiis automatically given to the module. The module system gives the module descriptor on the fly. fileThe module name,modi, is derived from the name of the JAR. -
The modi module needs
java.baseto run without error. -
2 packages in the modi project exist. In other words, the modi project consists of
com.example.modi.entrypointandcom.example.modi.querypackages. -
The main class,
com.example.modi.entrypoint.Main, runs when we use the module.
You can run the regular (nonmodular and unamed) JAR file in three ways:
$ java -cp jar/modi.jar com.example.modi.entrypoint.Main java.base (1) :) # or $ java -jar jar/modi.jar java.base (2) :)
-
We run the application on the classpath.
-
This is the conventional way of running a regular JAR file.
Keep in mind that, we cannot run a modular JAR with the same ways like above. Let’s try another approach now. This time, we move the JAR file from the classpath to the module path even the modi JAR is not a module however in fact, the JAR is going to be an automatic module.
$ java -p jar/modi.jar -m modi java.base (1) :)
-
We use
-por--module-pathto add our unnamed module ,modi.jarto the module path.--module-path(or short-form-p) is the new flag to work with modules (including unnamed modules as well).-mis the the short-form of--module. We should add the main class using the patternmodule-name/main-classwith the flag but since we already defined the main class in theMANIFESTfile, we don’t have to do that. Just giving the unnamed module name i.emodi(see the previous section for how themodiname is given), It resolves the main class from the givenmodi.jarwith the-pflag. We always need to use the-mflag at the end of the command along with its arguments if exists, e.g.-m jar.name args….