How to use --hash-modules and --module-path options of JDK 9 jar tool?

1.2k Views Asked by At

I would like to compute and record the hash of module matched by the given pattern that depend upon directly on a modular JAR file being updated. For this a use --hash-modules and --module-path options. There are my tries:

jar --hash-modules com.me.util --module-path "dist\com.me.jar;dist\com.me.util.jar"  --update --file dist/com.me.jar --main-class=com.me.A --verbose --module-version 0.1 -C build/modules/com.me module-info.class build/modules/com.me/com/me/A.class build/modules/com.me/com/me/B.class

jar --hash-modules "com.me.util;com.me.util" --module-path "dist\com.me.jar;dist\com.me.util.jar"  --update --file dist/com.me.jar --main-class=com.me.A --verbose --module-version 0.1 -C build/modules/com.me module-info.class build/modules/com.me/com/me/A.class build/modules/com.me/com/me/B.class

When I try to do it I get the warning message: "no module is recorded in hash in com.me".

These commands will create the *.jar files (modules) without any errors, but they won't add any hash information. I would like to see this information and take advantage of this functionality (--hash-modules and --module-path options). Please, tell me how to do it!


The complete structure of the project folders can be found here.

My experiments and working examples of using the jar tool's options are here.

The following operations described in Java Platform, Standard Edition Tools Reference (jar).

2

There are 2 best solutions below

3
On BEST ANSWER

Uddhav Gautam, thank you for the link to additional documentation.

After reading carefully the documentation (Packaging: Modular JAR files (JEP 261: Module System) and --hash-modules=PATTERN (Java Platform, Standard Edition Tools Reference)), I realized what parameters should be given to solve this task.

Here's a working example:

#Working command:
#Create module:
jar --hash-modules "com.me" --module-path "dist/com.me.jar" --verbose --create --file dist/com.me.util.jar -C build/modules/com.me.util module-info.class  build/modules/com.me.util/com/me/util/Util.class
jar --hash-modules "com.me" --module-path "dist/com.me.jar" -v -c -f dist/com.me.util.jar -C build/modules/com.me.util module-info.class  build/modules/com.me.util/com/me/util/Util.class

#Update module:
jar --hash-modules "com.me" --module-path "dist/com.me.jar" --verbose --update --file dist/com.me.util.jar -C build/modules/com.me.util module-info.class
jar --hash-modules "com.me" --module-path "dist/com.me.jar" -v -u -f dist/com.me.util.jar -C build/modules/com.me.util module-info.class

To see the result of the options, use the following command:

#Describe module:
jar --file dist/com.me.util.jar --describe-module

The result should be like this:

com.me.util jar:file:///C:/my_ch1_9/dist/com.me.util.jar/!module-info.class
exports com.me.util
requires java.base mandated
hashes com.me SHA-256 85c0539e4ca9a01b00f4c29a1a8b01cd452d1d97f437166b8bb415046dac65cb
1
On

--hash-modules <ProvidePatternHere> /* you are missing pattern */

http://openjdk.java.net/jeps/261

Hashes are only recorded for modules whose names match the regular expression

--module-path <LinkToModule>

So, a complete example would be something like below:

jar --hash-modules "*.jar" --module-path "dist" ... and your other stuffs here.