Build Your First Java Module

84 VIEWS

·

With Java 9 slated for release—hopefully—later this month, let’s look at one of its distinguishing features: the module. In my earlier post, I covered JShell, a Java REPL, and another interesting addition in Java 9. The module promises to streamline development, bringing some changes to how Java programs are developed, packaged, and shipped. However, there’s no need to panic. It is easy to learn to use and will bring tremendous utility to Java programmers.

What is a module? Simply put, it is a named, self-describing container for code and data. Hence, a module can contain Java code, native code, and data resources. The module solves a particular problem every Java developer is familiar with: Jar Hell. In addition, it provides greater encapsulation by determining which program components are accessible to others, thus redefining the meaning of public in Java. The module, hence, yields improved program performance and security.

Build Time

There are many aspects of the module worth exploring; however, I will only cover how to build a basic module from scratch. Note that Java 9 is required to complete this tutorial. Head here to download the JDK and JRE. You can explore the documentation as well.

It’s imperative you do this in a directory that doesn’t have too many folders so as not to get messy. Our working directory for this tutorial is first(feel free to use your own). Let’s begin.

Fire up your text editor and let’s define the module with the sample code below.

module first {

requires java.base;

}

This is what we have achieved in the above code: We have defined a module (called first), which depends on the module java.base. The java.base module is a special module that defines the foundational APIs for the Java platform and exports all the platform core packages. The whole requires statement can be omitted since only the base module is required. We are not done yet, as this code sample and all module definitions, have to be saved, by convention, in a file named module-info.java. Voila! You have a module. Let’s  add some code, compile, and run.

Create an App class in the package first from the directory with the module. The App class will do nothing complex. It will only display the current time.

package first;

public class App {

   public static void main(String[ ] args) {

System.out.println(java.time.LocalTime.now());

}     

}

From the same directory as the module, let’s compile and run our module from the command line.

$ javac -d . \

 module-info.java \

 first/App.java

$ java –module-path . first/App

 07:04:44.190947

As expected, the output is the current time. The -d in the first command specifies the output directory for the generated class files. The other files are the list of source files. The dot(.), in this case, represents the current directory. The –module-path option in the second command takes a list of directories, each representing a directory of modules; ours is the current directory(.). This is followed by the main class—App. There it is—a basic, working module.

There are more complexities that can be introduced to build more sophisticated modules. An exports clause can be added to a module definition to indicate the module makes available to other modules all the public types in a particular package, and can be packaged into a jar. To explore the module system, you can read this in-depth document by Mark Reinhold.

The module system is Java’s step toward embracing modularity for good. It has made it easier to scale the Java platform and the JDK to fit on small devices. Equally important, it made it simple to develop and maintain large applications. It is definitely one of the most distinguishing features in Java 9, and absolutely worth knowing.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu