Here at IDRsolutions we are very excited about Java 9 and have written a series of articles explaining some of the main features. In our previous Java 9 series article we looked at modularity in Java 9. This time we will be looking at jlink.
What is jlink?
Jlink is Java’s new command line tool which allows you to link sets of modules (and their transitive dependencies) to create a run-time image. Java has always had dynamic linking, but with Java 9 there is now an optional static linking step. This is called link time, and it happens between compile time and run time.
Why would I be interested in jlink?
Jlink will allow you to both simplify and reduce the size of deployment. The days of having to ship lots of jars and download the whole JRE/JDK are now long gone….
How do I use jlink?
This is the command you use to invoke the linker tool:
$ jlink --module-path <modulepath> --add-modules <modules> --limit-modules <modules> --output <path>
For example if I have an application called testapp in a directory called exampledir, and I want to create a run-time image in a directory called outputdir, I can use the following command:
$ jlink --module-path exampledir:$MODS --add-modules com.example.testapp --limit-modules com.example.testapp --output outputdir
Note that on windows the separator : should be replaced with ;
–module-path specifies where to find the modules for the JDK. These can be jar files, jmod files (a new file format with JDK 9 which is similar to the jar format but can handle native code and configuration files among other things) or exploded modules.
–add-modules adds the modules we need (in this example our app and nothing else)
–limit-modules limits to just the modules that we know our application needs (sometimes when modules are added, further modules can be added via transitive dependencies. Here we can specify that we only want certain modules)
–output is this directory where the run-time image will be
This command creates a run-time image which only has the modules that our application needs, but not the ones we don’t.
Other useful commands include:
$ jlink --help
to list more information about jlink and
$ jlink --version
to get the version number.
Jlink is a key part of the ‘jigsaw’ as Java moves away from being a monolithic language and becomes a much more modular and flexible environment. Will you be using it?