DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Java 9: Step by Step From Zero to Modules (Part 1)

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.

Tomer Ben David user avatar by
Tomer Ben David
·
Apr. 19, 17 · Tutorial
Like (64)
Save
Tweet
Share
80.85K Views

Join the DZone community and get the full member experience.

Join For Free

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

Image title


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.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SAST: How Code Analysis Tools Look for Security Flaws
  • Mr. Over, the Engineer [Comic]
  • Why Open Source Is Much More Than Just a Free Tier
  • Asynchronous HTTP Requests With RxJava

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: