Java 9 Modules Tutorial

Home / Developer Tools / Java 9 Modules Tutorial
Java 9 Modules Tutorial

1. Introduction

In this example, I would like to show you how to get started with Java 9 modules. Oracle JSR site has details of the features.

Java 9 is a major release. At the time of writing this article, JDK 9 is currently available for early access download on the oracle site and is expected to be released on July 27, 2017. This document attempts to summarize details of JDK 9 modules and list some of the main features with this new release. Complete list of features in Java 9 can be viewed at the oracle site.

Here, I present some examples and details to get started along with some of the important features and commands of this useful feature added in Java 9.

Table Of Contents

1. Introduction

2. Getting started with Java 9

3. What is a Java 9 module

3.1 Java 9 JDK structure

3.2 Writing a new module

3.3 View existing modules

3.4 Benefits of modules

4. Summary

5. References

2. Getting started with Java 9

To download the currently available early access JDK or JRE 9, visit http://jdk.java.net/9/.

Accept license

As shown in the image above, at the site, accept the license agreement and proceed to the download section as shown below.

Download JDK

Please select the appropriate OS and appropriate option for 32/ 64 bits for the OS to download the JDK/ JRE. It is also recommended to download the documentation along with the JDK/ JRE installation.

Tip
You may refer to this article to get started with Java 9 by executing a simple Hello World program.

3. What is a Java 9 module

From Oracle site:

A module is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

Project Jigsaw includes JSR 376 that is planned to include Java Modular system in Java 9. From the oracle site, the below are the objectives of Project Jigsaw.

The primary goals of this Project are to:

  • Make the Java SE Platform, and the JDK, more easily scalable down to small computing devices;
  • Improve the security and maintainability of Java SE Platform Implementations in general, and the JDK in particular;
  • Enable improved application performance; and
  • Make it easier for developers to construct and maintain libraries and large applications, for both the Java SE and EE Platforms.

JDK binaries have been getting bigger. One of the biggest advantages of the new module system is that the JDK and JRE get divided into smaller modules that are easy to scale down if needed. Also, you would notice that the JDK folder does not contain the JRE folder in Java 9. A new folder jmods has been added in Java 9.

3.1 Java 9 JDK structure

JDK installation in Java 9 has changed, you can view the folder structure below.

JDK folder structure

You can view any of the existing module folders to see the module-info.java file.

module-info.java

Also, if you go to the javalib folder in your JDK 9 installation, you will notice that tools.jar, rt.jar etc are missing. The lib folder looks like the below.

Java 9 lib folder

You can view the jmods folder to view the current modules. All jdk modules start with jdk*. The base module is the “java.base” module. This is also the biggest module under jmods folder.

jmods folder

3.2 Writing a new module

To get started with the creation of a new module, we must remember that a module has to have a unique name and needs to be defined in module-info.java. Also, the module-info.java class must be defined in a folder that has the same name as your module name. i.e. for the below module name, module-info.java must be placed in srccom.javacodegeeks.hellofolder.

First we create a new module descriptor:

module-info.java

1module com.javacodegeeks.hello

2{

3 

4}

This is a simple and a basic module descriptor, this is the module used further in the example.

We can also have module descriptors with exports and requires clauses. Examples below:

module-info.java

1module javacodegeeks.another.test

2{

3    exports com.javacodegeeks.test;

4 

5    requires com.javacodegeeks.test;

6 

7}

Here, requires clause specifies all the imports and exports clause specifies packages it exports to other modules. By default, all classes/ types inside a module are hidden to the outside world.

After creation of module descriptor, we will write a simple hello world class, this needs to go in the folder:

1srccom.javacodegeeks.hellocomjavacodegeekshello

HelloWorld.java

1package com.javacodegeeks.hello

2public class HelloWorld

3{

4    public static void main (String args [])

5    {

6        System.out.println (“Hello world”);

7    }

8}

To compile these via command line, run:

1javac -d modscom.javacodegeeks.hello com.javacodegeeks.hellomodule-info.java com.javacodegeeks.hellocomjavacodegeekshelloHelloWorld.java

Now, use this module:

1java –module-path mods -m com.javacodegeeks.hello/com.javacodegeeks.hello.HelloWorld

Output:

1Error occurred during initialization of boot layer

2java.lang.module.FindException: Error reading module: modscom.javacodegeeks.hello

3Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found intop-level directory (unnamed package not allowed in module)

The error we’ve made is that we missed the package declaration in HelloWorld class. Let us add that in and repeat!

1java –module-path mods -m com.javacodegeeks.hello/com.javacodegeeks.hello.HelloWorld

Output:

1Hello world

This is a very simple test for the module features in java 9.

3.3 View existing modules

To see all existing modules you can type:

1java –list-modules

The output in my version of JDK 9 early release download is:

01java.activation@9-ea

02java.base@9-ea

03java.compiler@9-ea

04java.corba@9-ea

05java.datatransfer@9-ea

06java.desktop@9-ea

07java.instrument@9-ea

08java.jnlp@9-ea

09java.logging@9-ea

10java.management@9-ea

11java.management.rmi@9-ea

12java.naming@9-ea

13java.prefs@9-ea

14java.rmi@9-ea

15java.scripting@9-ea

16java.se@9-ea

17java.se.ee@9-ea

18java.security.jgss@9-ea

19java.security.sasl@9-ea

20java.smartcardio@9-ea

21java.sql@9-ea

22java.sql.rowset@9-ea

23java.transaction@9-ea

24java.xml@9-ea

25java.xml.bind@9-ea

26java.xml.crypto@9-ea

27java.xml.ws@9-ea

28java.xml.ws.annotation@9-ea

29javafx.base@9-ea

30javafx.controls@9-ea

31javafx.deploy@9-ea

32javafx.fxml@9-ea

33javafx.graphics@9-ea

34javafx.media@9-ea

35javafx.swing@9-ea

36javafx.web@9-ea

37jdk.accessibility@9-ea

38jdk.attach@9-ea

39jdk.charsets@9-ea

40jdk.compiler@9-ea

41jdk.crypto.cryptoki@9-ea

42jdk.crypto.ec@9-ea

43jdk.crypto.mscapi@9-ea

44jdk.deploy@9-ea

45jdk.deploy.controlpanel@9-ea

46jdk.dynalink@9-ea

47jdk.editpad@9-ea

48jdk.hotspot.agent@9-ea

49jdk.httpserver@9-ea

50jdk.incubator.httpclient@9-ea

51jdk.internal.ed@9-ea

52jdk.internal.jvmstat@9-ea

53jdk.internal.le@9-ea

54jdk.internal.opt@9-ea

55jdk.internal.vm.ci@9-ea

56jdk.jartool@9-ea

57jdk.javadoc@9-ea

58jdk.javaws@9-ea

59jdk.jcmd@9-ea

60jdk.jconsole@9-ea

61jdk.jdeps@9-ea

62jdk.jdi@9-ea

63jdk.jdwp.agent@9-ea

64jdk.jfr@9-ea

65jdk.jlink@9-ea

66jdk.jshell@9-ea

67jdk.jsobject@9-ea

68jdk.jstatd@9-ea

69jdk.localedata@9-ea

70jdk.management@9-ea

71jdk.management.agent@9-ea

72jdk.naming.dns@9-ea

73jdk.naming.rmi@9-ea

74jdk.net@9-ea

75jdk.pack@9-ea

76jdk.packager@9-ea

77jdk.packager.services@9-ea

78jdk.plugin@9-ea

79jdk.plugin.dom@9-ea

80jdk.plugin.server@9-ea

81jdk.policytool@9-ea

82jdk.rmic@9-ea

83jdk.scripting.nashorn@9-ea

84jdk.scripting.nashorn.shell@9-ea

85jdk.sctp@9-ea

86jdk.security.auth@9-ea

87jdk.security.jgss@9-ea

88jdk.snmp@9-ea

89jdk.unsupported@9-ea

90jdk.xml.bind@9-ea

91jdk.xml.dom@9-ea

92jdk.xml.ws@9-ea

93jdk.zipfs@9-ea

94oracle.desktop@9-ea

95oracle.net@9-ea

As you can see in the output above, JDK comes bundled with many modules. java.base module is the base module for all other modules.

3.4 Benefits of modules

Modules in java promise to make java light-weight, bring in reusability and make java easier to manage and debug. With Java becoming light-weight, it would allow Java to run on more devices.

Greater speed of development is expected with use of modules. Resources may be more effectively used in Java 9 with use of modules.

Another major feature of modules is encapsulation – modules would be given a safe place to run. We can specify explicit dependencies along with strong encapsulation.

4. Summary

We looked at some of the important reasons to use java 9 modules. Java 9 modules will allow for dynamic runtime configuration and easy encapsulation. It is a way out from current applications that use classpaths with many jars and often debugging is difficult.

5. References

http://www.journaldev.com/13106/javase9-module-system-part1
https://www.pluralsight.com/blog/software-development/java-9-new-features
https://www.romexsoft.com/blog/java-8-vs-java-9/
https://www.pluralsight.com/courses/java-9-modularity-first-look
Keynote session by Mark Reinhold

 

Source: https://examples.javacodegeeks.com/core-java/java-9-modules-tutorial/

Leave a Reply

Your email address will not be published.