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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • How to Build Your Own AI SRE From Scratch
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
Tomer Ben David
·
Apr. 19, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
82.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

[email protected]:~$ 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)
[email protected]:~$ cd ~/tmp
[email protected]:~$ mkdir -p ~/tmp/java9-modules
[email protected]:~$ 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:

[email protected]:~/tmp/java9-modules$ mkdir -p src/com.me.mymodule
[email protected]:~/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.

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook