Samples

The Modules Project was inactive and was subsequently dissolved Oct 2023. The approach described here was superseded by Project Jigsaw.

The source bundle of the Module project includes samples in the directory src/share/sample/modules. This page explains what they do and how to try them out. We assume you have built the JDK as described in the getting started document and added its bin directory to the beginning of your path.

Hello World

The first sample is contained in the hello-world directory is the obligatory Hello World program:

% cd src/share/sample/modules/hello-world
% javac hello/*.java
% jam cfs hello.jam hello hello/*.class
% java -jam hello.jam
Hello world from module hello 1.0

Explanation: first we compile all sources in the hello package. Then we call the jam command to create a JAM archive from the class files. Finally, we call the Java launcher to execute the archive hello.jam, which prints the Hello world message.

Let's look at the source files, first hello/Main.java:

package hello;

public class Main {

    public void run(String[] args) {
        System.out.println("Hello world from " + getClass().getClassLoader().getModule());
    }

    public static void main(String[] args) {
        new Main().run(args);
    }
}

It is class with a main that prints the hello-world message. It also calls the getModule() method on the class' ClassLoader, which for classes that were loaded from a module returns the Module object. This Module object can be used at runtime to inspect its properties, to obtain the module's repository reflectively instantiate other modules, etc.

The other source file is the superpackage declaration for the module. Because superpackages are not yet supported by javac, a special class declaration in the form described on this page is used instead. However, here we will show how the superpackage for this sample will be declared once superpackages are implemented in javac:

File hello/super-package.java.

import java.module.annotation.*;

@MainClass("hello.Main")
@Version("1.0")
superpackage hello {

    member package hello;

    export hello.Main;

    uses java.se;
}

A superpackage defines the module name, its members and exported types, and metadata in the form of annotations. In this example, the superpackage is called hello and its member are the package hello and its types. The class hello.Main is an exported type, and the core platform module java.se is a dependency of the module. (Note that how superpackages will capture dependencies in an issue currently being discussed in the JSR 294 EG. It may not be via an explicit "uses" statement) Annotations declare the version of the module and the main class to be started by the launcher, hello.Main.

Dependencies

The second sample demonstrates module dependencies and the URL repository. It is contained in the dependencies directory and built in much the same way as the hello world sample, see the comments in dep/Entry.java for details. For convenience, there is also a built version with JAM files setup as a URL repository in the dependencies/urlrepository directory.

A module can declare dependencies on other modules. The dep module declares a dependency on the hello module in dep/super_package.java. The module system examines this information when the module dep is initialized and makes sure that an appropriate version of the module hello is present. It also sets up the class loaders appropriately.

To try this out, run:

% cd src/share/sample/modules/dependencies
% java -repository urlrepository -module dep
Module 'dep' calling module 'hello'...
Hello world from module hello 1.0

Note that it was not necessary to specify the hello module using a -classpath option.

The URL repository is also suitable for deployment over the network and can be used to directly launch remote applications. This sample repository is also available on the OpenJDK web site. That allows the dep application can be started using:

% java -repository https://openjdk.org/projects/modules/samplerepo/ -module dep
Module 'dep' calling module 'hello'...
Hello world from module hello 1.0

However, remember that running untrusted code without a security manager - as done in this example - is not recommended. Currently the security permission for this application would have to be configured manually, but this may be addressed by the Module system in the future.