Return to site

JAVA CERTIFICATION QUESTION: Modules and JARs

ยท java,ocp

Assume you have an application with the main class ๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’.

This application is packaged in a JAR file named ๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ and depends on a JAR called ๐’‡๐’๐’.๐’‹๐’‚๐’“, which uses types in ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“.

The ๐’‡๐’๐’.๐’‹๐’‚๐’“ has just been modularized into a module named ๐’‡๐’๐’.

You decide to turn your JAR file into a module with name ๐’Ž๐’š๐’‚๐’‘๐’‘.

๐Ÿ™‹โ€โ™€๏ธ Given both module declarations are valid, which two of the following are the correct commands to execute your application?

* ๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ ๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

* ๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

* ๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“ -๐’‘ ๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

* ๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“ -๐’‘ ๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

* ๐’‹๐’‚๐’—๐’‚ -๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

๐Ÿ’กAnswer:

* ๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ ๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

* ๐’‹๐’‚๐’—๐’‚ -๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’

๐Ÿ‘ฉโ€๐Ÿซ Explanation:

When ๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ is placed on the module path, neither ๐’‡๐’๐’.๐’‹๐’‚๐’“ nor ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“ can be on the classpath.

The reason is that a named module cannot read packages in the unnamed module.

This means options

๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“ -๐’‘ ๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’ and

๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“ -๐’‘ ๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’ are wrong.

Then ๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’ is incorrect because when ๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ is on the classpath ,it's considered part of the unnamed module.

This means there would be no module named ๐’Ž๐’š๐’‚๐’‘๐’‘.

๐’‹๐’‚๐’—๐’‚ -๐’„๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ ๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’ is correct since both modular JARs will be considered regular JARs, and the application will run as if it was on a non-modular system.

๐’‹๐’‚๐’—๐’‚ -๐’‘ ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“;๐’‡๐’๐’.๐’‹๐’‚๐’“;๐’Ž๐’š๐’‚๐’‘๐’‘.๐’‹๐’‚๐’“ -๐’Ž ๐’Ž๐’š๐’‚๐’‘๐’‘/๐’Ž๐’š๐’‚๐’‘๐’‘.๐‘ด๐’‚๐’Š๐’ is correct too, as ๐’ƒ๐’‚๐’“.๐’‹๐’‚๐’“ becomes an automatic module when placed on the module path. An automatic module exports all of its packages, hence, can be used by foo.jar.

#java #certificationquestion #ocp

๐ŸŽ“๐Ÿ‘‰