Well, Java 9 is available so let’s make modules!

Modularity is important in Java9! In the future, modules will be the basic building units of applications. Whether written in a dynamic or static language, a module is an important part of an application and follows the principle of a single function. You can call any method a module, just as functional programming is based on modules. The important thing is to need to be said 3 times.

Steps:

  • Step 1: Download Java 9 and install it
  • Step 2: Create a module Java file
  • Step 3: Module file path
  • Step 4: Write a module
  • Step 5: Add code to our module
  • Step 6: Compile our module
  • Step 7: Run our module

1. Download and install Java 9

Download address: www.oracle.com/technetwork…

Once downloaded, just click on it to install (if you are on MacOS) and confirm that it is installed:

tomerb@tomerb-mac.local:~$ java --version
java 9-ea
Java(TM) SE Runtime Environment (build 9-ea+164)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+164, mixed mode)
tomerb@tomerb-mac.local:~$ cd ~/tmp
tomerb@tomerb-mac.local:~$ mkdir -p ~/tmp/java9-modules
tomerb@tomerb-mac.local:~$ cd ~/tmp/java9-modulesCopy the code

2. Create a module Java file

In Java 9, to define modules, you need to give the Java file a special name. The recommended name is module-info.java

3. Module file path

Where does module-info.java go? By convention, it should be placed in the same directory as the module name.

If your module name iscom.me.mymoduleSo your module module-info.javaIt should be placed in: SRC /com.me.mymoduleThis makes your module-info.javaFile in: SRC /com.me.mymodule/module-info.javaThe path. Got it? <module-path> == <module name>Copy the code

4. Write a module

Now that we know our module filename and our module filepath, let’s write a module with this naming and folder convention:

tomerb@tomerb-mac.local:~/tmp/java9-modules$ mkdir -p src/com.me.mymodule
tomerb@tomerb-mac.local:~/tmp/java9-modules$ vi src/com.me.mymodule/module-info.javaCopy the code
module com.me.mymodule{}Copy the code

At this point, we are finished writing a module!

5. Add code to our module

In this step, we will add some code to our module! Create a new Java file in the same directory as our module:

$ mkdir -p src/com.me.mymodule/com/me/mymodule
$ vi src/com.me.mymodule/com/me/mymodule/Main.javaCopy the code

Notice the directory name in our code. Why do you do that? We first enter the path where our module is, and then we create the full package name for the source code. In this case, /com/me/ myModule is above /com.me. myModule. It’s just that our source files belong to our module, and the module is already in the standard module directory of the Java 9 convention.

So, we’ll write Hello World in main.java:

package com.me.mymodule;
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World from Module! :)"); // nothing special here, standard java < 9 class.}}Copy the code

6. Compile our module

First make the mods directory (which we will pass further to: Java –module-path) :

$ mkdir -p mods/com.me.mymodule
$ javac -d mods/com.me.mymodule \
          src/com.me.mymodule/module-info.java \
          src/com.me.mymodule/com/me/mymodule/Main.javaCopy the code

Tap your noble fingers and let the compiler run a little longer!

7. Run our module

$ java --module-path mods -m com.me.mymodule/com.me.mymodule.Main
Hello World from Module! :)Copy the code

conclusion

In this section, we download Java 9, create a module, add a source file to it, and run it. We see that there is a naming convention to follow when creating module paths and the like when creating source code.

Original text: dzone.com/articles/ja…