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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
philippe
Posts: 26
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

philippe
Posts: 26
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!

Thetri_enth
Posts: 4
Joined: Tue May 03, 2022 7:50 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests