Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 22
Question ID: UKOCP77559
Given below the directory/file structure on Windows platform:
- C:
- ├───src
- │ └───<MODULE_DIRECTORY_NAME>
- │ │ module-info.java
- │ │
- │ └───com
- │ └───messages
- │ Test.java
- │
- └───out
Below is the code of Test.java file:
- package com.messages;
- public class Test {
- public static void main(String [] args) {
- System.out.println("COMMIT OR QUIT");
- }
- }
And below is the code of module-info.java file:
- module com.messages {
- }
And below commands:
javac --module-source-path src -d out --module com.messages
java -p out -m com.messages/com.messages.Test
Which of the following options replaces <MODULE_DIRECTORY_NAME> such that output of the 2nd command is: COMMIT OR QUIT?
- A messages
- B com.messages
- C mymodule
- D messages-module
- E com.messages-module
- F com.messages.module
Reveal correct answer
Correct answer: B
Explanation
UKOCP77559:
Format of javac command is:
javac <options> <source files>
Below are the important options (related to modules) of javac command:
--module-source-path <module-source-path>:
Specify where to find input source files for multiple modules. In this case, module source path is 'src' directory.
--module <module-name>, -m <module-name>:
Compile only the specified module. This will compile all *.java file specified in the module. Multiple module names are separated by comma.
-d <directory>:
Specify where to place generated class files. In this case, it is 'out' directory.
--module-path <path>, -p <path>:
Specify where to find compiled/packaged application modules. It represents the list of directories containing modules or paths to individual modules. A platform dependent path separator (; on Windows and : on Linux/Mac) is used for multiple entries. For example, javac -p mods;singlemodule;connector.jar represents a module path which contains all the modules inside 'mods' directory, exploded module 'singlemodule' and modular jar 'connector.jar'.
It is not used in this case as given module is not dependent upon other compiled/packaged application modules.
Format of the java command related to module is:
java [options] -m <module>[/<mainclass>] [args...]
java [options] --module <module>[/<mainclass>] [args...]
(to execute the main class in a module)
--module or -m: It corresponds to module name. -m should be the last option used in the command. -m is followed by module-name, then by optional mainclass and any command-line arguments. If module is packaged in the jar and 'Main-Class' attribute is set, then passing classname after -m <module> is optional.
And below is the important option (related to modules) of java command:
--module-path or -p: It represents the list of directories containing modules or paths to individual modules. A platform dependent path separator (; on Windows and : on Linux/Mac) is used for multiple entries.
For example, java -p out;singlemodule;connector.jar represents a module path which contains all the modules inside 'out' directory, exploded module 'singlemodule' and modular jar 'connector.jar'.
Given javac command:
C:\>javac --module-source-path src -d out --module com.messages => This is multi-module compilation command and for this command to work, module name and module directory name must be same.
Module name defined in module-info.java file is: 'com.messages'. Hence only com.messages can replace <MODULE_DIRECTORY_NAME>.
Please note that module names live in a global namespace, separate from other namespaces in Java. Hence, module name can use the name of the package, class or interface.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
