Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium

Question 6

Question ID: UKOCP80724


Given below the directory/file structure on Windows platform:


Contents of C:\src\messages\com\udayankhattry\ocp\Main.java file:


Contents of C:\src\messages\com\udayankhattry\ocp\Test.java file:


Contents of C:\src\messages\module-info.java file:


'messages.jar' file was created by executing below command from C:\

jar -cfe jars\messages.jar com.udayankhattry.ocp.Test -C bin\messages .


Which of the following commands, if executed from C:\, displays FOCUS AND WIN on to the console?

Choose 2 options.

  • A

    java -p jars\messages.jar -m messages

  • B

    java -p jars -m messages

  • C

    java -p bin -m messages

  • D

    java -p bin -m messages/com.udayankhattry.ocp.Main

  • E

    java -p jars -m messages/com.udayankhattry.ocp.Main

  • F

    java -m messages/com.udayankhattry.ocp.Main -p jars

  • G

    java -m messages/com.udayankhattry.ocp.Main

Reveal correct answers

Correct answers: D, E

Explanation

UKOCP80724:

Options of the jar command:

-c or --create: Create the archive

-f or --file=FILE: The archive file name

-e or --main-class=CLASSNAME: The application entry point for stand-alone applications bundled into a modular, or executable, jar archive

-C DIR: Change to the specified directory and include the following file


Given jar command:

C:\>jar -cfe jars\messages.jar com.udayankhattry.ocp.Test -C bin\messages .

Jar command instructs to create a new archive 'jars\messages.jar' and set the entry point to 'com.udayankhattry.ocp.Test' class.

-C bin\messages . instructs jar tool to change to the 'bin\messages' directory and put all compiled files from this directory in the JAR file.


Important point to note here is that entry point (mainclass) is: com.udayankhattry.ocp.Test


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


Let's check all the options one by one:

java -p jars\messages.jar -m messages

✗  As <mainclass> has not been specified, hence it considers the mainclass set at jar creation, which is: com.udayankhattry.ocp.Test. Output is: DRILL YOUR SKILLS


java -p jars -m messages

✗  With -p option, you can specify directory containing modules or individual modules as well. Same as above, it also prints DRILL YOUR SKILLS on to the console


java -p bin -m messages

✗  As directory 'bin' contains exploded module, hence class name must be specified after module name 'messages'


java -p bin -m messages/com.udayankhattry.ocp.Main

✓  As directory 'bin' contains exploded module and class name is also specified after module name 'messages', main(String...) method of Main class gets executed and prints FOCUS AND WIN on to the console.


java -p jars -m messages/com.udayankhattry.ocp.Main

✓  Specifying class name after the module name (in case of jar file) executes the given class and not the entry-point (mainclass) set at jar creation. main(String...) method of Main class gets executed and prints FOCUS AND WIN on to the console.


java -m messages/com.udayankhattry.ocp.Main -p jars

✗  -m must be the last option.


java -m messages/com.udayankhattry.ocp.Main

✗  --module-path or -p option is missing.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need