Page 1 of 1

About Question enthuware.ocpjp.ii.v11.2.3439 :

Posted: Thu Jan 14, 2021 9:38 pm
by philippe
The following option is incorrect:

Code: Select all

java -classpath reporting-5.6.jar --module-path accounting-3.3.jar; com.abc.reporting.Main
Because of:

"Since the Main class is loaded from reporting jar, which is not a modular jar, the JVM does not know which modules are required by this class. So, even though accounting jar is on the module-path, it will not be loaded and so, classes from accounting jar will not be found."

The following option is correct:

Code: Select all

java  --module-path accounting-3.3.jar;reporting-5.6.jar --module reporting/com.abc.reporting.Main
Why will the accounting jar be found in this option, but not in the previous one? In both cases the accounting jar is just on the module path, i.e. not specified via --add-modules.

Re: About Question enthuware.ocpjp.ii.v11.2.3439 :

Posted: Thu Jan 14, 2021 11:23 pm
by admin
The first command line is not running a module. It is executing a class directly in the old fashion.
The second command is executing a module using --module option.

Re: About Question enthuware.ocpjp.ii.v11.2.3439 :

Posted: Sun Jan 17, 2021 5:48 pm
by philippe
I've tested this out. Also for the second command where both jars are put on the module path, --add-modules must be specified:

Code: Select all

[jars] $ java --module-path accounting.jar:reporting.jar -m reporting/reporting.Main
Exception in thread "main" java.lang.NoClassDefFoundError: accounting/Account
        at reporting/reporting.Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: accounting.Account
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
        ... 1 more
[jars] $ java --module-path accounting.jar:reporting.jar --add-modules accounting -m reporting/reporting.Main
0
Link to archive with source code and jars: https://we.tl/t-xwouOqMHmj

Re: About Question enthuware.ocpjp.ii.v11.2.3439 :

Posted: Sun Jan 17, 2021 11:44 pm
by admin
You are right. --add-modules is required in option 4. Since reporting.jar is an automatic module, it doesn't have any module-info and so, java cannot determine the modules that it requires and so, java does not load the modules in other jars (even though they are accessible/readable) to classes in reporting.jar.
Updated.
thank you for your feedback!

Re: About Question enthuware.ocpjp.ii.v11.2.3439 :

Posted: Mon May 23, 2022 7:19 am
by Thetri_enth
If the reporting team hasn't decided to modularize their jar, why is the option with the automatic module correct? Is it only 'modularizing a jar' when you create a named module?

Re: About Question enthuware.ocpjp.ii.v11.2.3439 :

Posted: Mon May 23, 2022 7:38 am
by admin
That is how the top-down approach works. If the reporting team hasn't decided to modularize their jar, that shouldn't stop you from modularizing your application. When you put the reporting jar on the module-path, your application will be able to use classes in the reporting jar without the need for that team to modularize it. Otherwise, classes in your application (which you are modularizing) will not be able to access classes in reporting jar.