InterviewSolution
| 1. |
Create a module in Java 9 |
|
Answer» A self-explanatory COLLECTION of code and resources is called a MODULE. It adds a higher level of hierarchy above packages. A module is basically a uniquely named, RECLAIMABLE group of related packages, as well as resources (such as images and XML FILES) and a module descriptor. Without further ado, let us go on and see the Steps to create a module in Java 9. Let the name of our module be examplemodule. C:\>Java\src The same folder is visible here: Create a folder named examplemodule inside the src folder: C:\Java\src\examplemoduleThe screenshot displays the same path: Step 2: Create a module-info.java file in the C:\>Java\src\examplemodule folder with following code: module examplemodule{ }The screenshot: Step 3: Create a file Example.java in the C:\Java\src\examplemodule folder. The file has the following SOURCE code: package examplemodule; public class Example { public static void main(String[] args) { System.out.println("Welcome to Java 9"); } }Step 4: Create a folder C:\Java\mods. Create a folder name examplemodule in the mods folder. This is the same as the name of the module we have created. Now compile the module to mods directory C:\Java> javac -d mods/examplemodule src/examplemodule/module-info.java src/examplemodule/Example.javaStep 5: Let us run the module by running the following command C:\Java>java --module-path mods -m examplemodule/examplemodule.Example The module-path provides the module location as mods and -m signifies the main module. The following is the output: |
|