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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Trending

  • Data Quality: A Novel Perspective for 2025
  • Agile’s Quarter-Century Crisis
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  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 user avatar
Tomer Ben David
·
Apr. 19, 17 · Tutorial
Likes (64)
Comment
Save
Tweet
Share
82.2K 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.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!