Page 1 of 1

[HD Pg 0, Sec. 16.3.1 - enabling-access-between-modules]

Posted: Wed Feb 26, 2020 8:07 pm
by nk2164
In this section, you talk about creating multiple modules (simpleinterest and calculator) and then executing them. I wanted to know if its possible to create a single jar file that includes both these modules and then execute them . Is that possible ?

I tried the following, but the resulting jar file only had simpleinterest class and its corresponding module-info.java

Code: Select all

jar --create --file simpleinterest.jar --main-class simpleinterest.SimpleInterestCalculator -C out/simpleinterest .
Then i tried this

Code: Select all

jar --create --file simpleinterest.jar --main-class simpleinterest.SimpleInterestCalculator -C out .
This bought in both the modules into simpleinterest.jar but i was not able to get it to run.

Re: [HD Pg 0, Sec. 16.3.1 - enabling-access-between-modules]

Posted: Thu Feb 27, 2020 1:52 am
by admin
You can't have two modules in the same jar file. The book notes on page 402 under Packaging a module:
The only thing special about a module jar is that the name of the jar is, by convention, the same as the name of the module (with .jar extension, of course). Inside the jar file, class files must still exist in their package driven directory structure just like before. Observe that a module also has a module-info.class in its root folder. This class must be in the root folder of the module’s jar as well.
Since there is a module-info.class for each module but only one module-info.class can reside in the root of a jar, it is not possible to put two modules in the same jar.

Of course, you can create a regular jar and put all the classes in it and run it as a regular jar but you can't use module options with this jar.

Re: [HD Pg 0, Sec. 16.3.1 - enabling-access-between-modules]

Posted: Thu Feb 27, 2020 9:13 am
by nk2164
Thank you for the clarification. One last question - for packing applications into formats like EAR's WAR's etc , i can still package these multiple modular jars just like today right ? I would think this should not change.

Re: [HD Pg 0, Sec. 16.3.1 - enabling-access-between-modules]

Posted: Thu Feb 27, 2020 9:23 am
by admin
Yes, there is no difference in the internal structure of a module jar and a regular jar. You can use a module jar just as you use a regular jar.

Re: [HD Pg 0, Sec. 16.3.1 - enabling-access-between-modules]

Posted: Thu Feb 27, 2020 9:27 am
by nk2164
Great. Thank you so much.