Category Archives: Java 9

Module-Info Syntax

[open] module <MODULE_NAME> {
    [[requires [transitive] <MODULE_NAME>;]
    [exports <PACKAGE>[ to <MODULE_NAME_1>[,<MODULE_NAME_2>..,<MODULE_NAME_N>];]
    [opens <PACKAGE>;]
    [uses <SERVICE>;]]
Identifier Description
module The module keyword that holds the module directives.
<MODULE_NAME> The module identier.
<PACKAGE> The package identifier.
requires The requires directive indicates that this module depends on another module. ^1
transitive The transitive directive indicates that the user of the current module will also requite another module that the current user will also have access into.
exports The exports directive indicates which public types of the module's package are accessible to other modules. ^1
opens The opens directive also indicates which public types of the module's package are accessible to other modules. The difference between that and exports is that opens is not for compile time, only during runtime, while exports is for both compile time and runtime. The opens directive can typically be used when you want to allow other modules to use reflection for the types in the specified packages, but not to use them during compile time. Let's make this more clear by means of an example. ^1
open This is like the opens directive but with this it opens the whole module.
to <MODULE_NAME_1>[, <MODULE_NAME_2>.., <MODULE_NAME_N>] This indicates to target just specific module(s).
provides...with The provides...with directive indicates that the module provides a service implementation.^1
uses The uses directive indicates that the module requires a service implementation.
<SERVICE> The interface or abstract class that will be implemented as a service.

Using Embedded Derby in Java 9 with Gradle

  1. Add the following dependencies to your build.gradle file:
    compile group: 'org.apache.derby', name: 'derby', version: ''
    compile group: 'org.apache.derby', name: 'derbyshared', version: ''
  2. Add the following entries to your file:
    requires org.apache.derby.engine;
    requires org.apache.derby.commons;
    requires java.sql;
  3. In your Java class, you can create a connection like the following:
    final String DATABASE_NAME = "sample_table";
    String connectionURL = String.format("jdbc:derby:%s;create=true", DATABASE_NAME);
    connection = DriverManager.getConnection(connectionURL);
  4. Do your needed database operations with the connection (e.g. create table, execute a query, or call a stored procedure.).
  5. When your done using the connection, close the connection and shutdown the Derby engine like the following:
    boolean gotSQLExc = false;
    try {
        //shutdown all databases and the Derby engine
    } catch (SQLException se)  {
        if ( se.getSQLState().equals("XJ015") ) {
            gotSQLExc = true;
    if (!gotSQLExc) {
        System.out.println("Database did not shut down normally");

    A clean shutdown always throws SQL exception XJ015, which can be ignored.

Getting a Resource using ModuleLayer

If the resource and the module are known, we can use the ModuleLayer to access it like the following snippet.

ModuleLayer.boot().findModule(<MODULE_NAME>).ifPresent(___module -> {
    try {
        var modResource = ___module.getResourceAsStream(<RESOURCE_NAME>);
    } catch (IOException e) {


MODULE_NAME -> the name of the module that has the resource.
RESOURCE_NAME -> the resource of interest.

Sample Usage of Java 9 Flow (Reactive Stream) API

Reactive stream is gaining traction in the mainstream programming and java has its own implementation via the Flow API. Popular reactive stream implementations are RxJava, Reactor and Akka.

import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;

public class Main {

     * Sample subscriber implementation.
    public static class Subscriber implements Flow.Subscriber<Integer> {

         * Holds an instance of Flow.Subscription instance so that we can request what we can handle.
        private Flow.Subscription subscription;

         * Tracks if the publisher was closed.
        private boolean isDone;

         * Triggered on the initial subscription.
         * @param subscription An instance of Flow.Subscription.
        public void onSubscribe(Flow.Subscription subscription) {
            this.subscription = subscription;

         * Do the actual processing.
         * @param item The actual item currently being processed.
        public void onNext(Integer item) {
            System.out.println("Processing " + item);

         * Holds how to handle error.
         * @param throwable An instance of Throwable.
        public void onError(Throwable throwable) {

         * Called with the publisher was closed or completed.
        public void onComplete() {
            System.out.println("Processing done.");
            isDone = true;

    public static void main(String[] args) throws InterruptedException {
        //The publisher of the data.
        SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>();

        //The sample subscriber implementation.
        Subscriber subscriber = new Subscriber();
        //Register a subscriber.

        //The sample stream to process.
        var intData = IntStream.rangeClosed(1, 10);

        //Publish the stream data. 

        //The publisher is done.

        //Since this is processing is asynchronous wait for everything to be processed. 
        while(!subscriber.isDone) {

Using JShell for testing an API

Java 9 was released and JShell (i.e. The REPL of Java) is now real and we can use this to test an API.

For example if we want to check the reverse function StringUtils from the commons-lang3-3.1.jar.
1.) Using windows terminal (i.e. cmd command).
2.) From this directory execute to following command jshell command.
jshell --class-path .\commons-lang3-3.1.jar
Expect to see something similar to the following:
Welcome to JShell -- Version 9.0.1
For an introduction type: /help intro

3.) From the jshell prompt import the class org.apache.commons.lang3.StringUtils just like we normally do in java and then press enter.
jshell> import org.apache.commons.lang3.StringUtils
4.) To optionally check all the imported classes on the current jshell session we can use /imports command.
jshell> /imports
The sample output would be:
|    import*
|    import java.math.*
|    import*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import*
|    import org.apache.commons.lang3.StringUtils
5.) Since we know that the StringUtils class was already loaded. We can now execute its reverse function.
jshell> StringUtils.reverse("abcde")
The output must be:
$2 ==> "edcba"
Note: if you want to exit jshell execute the /exit command.