Java 9: Step by Step From Zero to Modules (Part 1)
Well, Java 9 is around the corner, so it makes sense to get started with making modules! This guide covers the basic building blocks of making modules.
Join the DZone community and get the full member experience.
Join For FreeWe all know why modules
are super important. Modules are the building blocks of microservices
! Modules the building blocks of a well-organized application. No matter whether you are writing in a dynamic language or a static language, modules are a super important part of your application! Modules enforce the single responsibility principle. You can call every function a module, as functional programming is based around modules. OK, you get me, modules are important, and this is why we are going to focus on them in this part of our tutorial.
Our plan:
Step 1: Download Java 9 from scratch and install
Step 2: Concept: Describe our module filename
Step 3: Concept: Describe our module filepath
Step 4: Code our module descriptor file:
module-info.java
Step 5: Add code to our module
Step 6: Compile our module
Step 7: Run our module
Step 1: Download Java 9
Goto: https://jdk9.java.net/download/ and click on the jdk
relevant to your OS
.
And after downloading it, just click it to install (if you are on macOS) and verify that you have it 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-modules
Step 2: Module Filename
In Java 9, you need to have a specific file name by convention in order to define modules. Yes, a specific filename. That filename should be called: module-info.java
.
Step 3: Module Filepath
Now, where do you put this file module-info.java
,? Well, by convention, you place it in a folder that has the same name as your module name. So if your module name is
com.me.mymodule
Then your module that is your module-info.java
should be placed in:
src/com.me.mymodule
Which makes your module-info.java
file placed at:
src/com.me.mymodule/module-info.java
path.
Got it? <module-path> == <module name>
!
Step 4: Let’s Code a Module
Now that we know our module filename
and our module filepath
, let’s code a module with that naming and folder convention in hand:
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.java
module com.me.mymodule { }
This is it! We have a module! (With no real interesting implementation. That’s next.)
Step 5: Add Some Code to Our Module!
In this step, we are going to add some code to our module! So let’s create a new java
file in the same folder as our module.
$ mkdir -p src/com.me.mymodule/com/me/mymodule
$ vi src/com.me.mymodule/com/me/mymodule/Main.java
Note the folder name we place in our source code. Is it strange? We first enter the path our module resides in and then we create the full package name for our source code. In this case, /com/me/mymodule
this is on top of /com.me.mymodule
. It’s just that our source file belongs to our module and the module is already in a standard modules dir, by Java 9 convention.
So the source for Main.java
would be a simple hello world source.
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.
}
}
Step 6: Compile Our Module
First make the mods
dir (that we would further on pass 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.java
Nest we cross our fingers, and let the compiler... compile!
Step 7: Run Our Module!
$ java --module-path mods -m com.me.mymodule/com.me.mymodule.Main
Hello World from Module! :)
Summary
In this part, we downloaded Java 9, created a module, added a source file to it, and ran it. We saw that there is a naming convention to be followed while creating a module path and something similar when creating the source code.
Opinions expressed by DZone contributors are their own.
Comments