How do I run images generated by JDK 9 jlink?

clay picture clay · Sep 25, 2017 · Viewed 7.1k times · Source

I follow the Jigsaw quickstart here. I successfully ran the jlink command given:

jlink --module-path $JAVA_HOME/jmods:mlib --add-modules com.greetings --output greetingsapp

That produces a "runtime image" which is an exploded directory structure that looks like:

~ tree -d greetingsapp
greetingsapp
├── bin
├── conf
│   └── security
│       └── policy
│           ├── limited
│           └── unlimited
├── include
│   └── darwin
├── legal
│   └── java.base
└── lib
    ├── jli
    ├── security
    └── server

How do I run this? I was expecting a binary executable, not an exploded directory tree.

The bin directory has a java and a keytool. I don't see any .jar files or .class files to run via the bundled java executable.

Answer

Naman picture Naman · Sep 25, 2017

To run, do this:

greetingsapp/bin/java -m com.greetings/com.greetings.Main 

Or, you can have jlink build a launcher script that does this

jlink --module-path $JAVA_HOME/jmods:mlib --add-modules com.greetings --output greetingsapp --launcher launch=com.greetings/com.greetings.Main

and then run with:

greetingsapp/bin/launcher

Form the same documentation :-

$ java -p mods -m com.greetings/com.greetings.Main

could be executed to run the Main class from the module structure without linking using jshell as well.


Also, jlink is the linker tool and can be used to link a set of modules, along with their transitive dependencies, to create a custom modular run-time image called as Modular Runtime Images which can be accomplished using the JMOD tool introduced with Java 9 modules. As pointed out in comments and answered by @Jorn if you simply intend to execute the main class.

You can run your application by using the java binary in the bin folder of the generated image, and using the command:

java com.greetings.Main

On the other hand, an example of creating a JMOD file to be used as a module further is as :

jmod create --class-path mods/com.greetings --cmds commands
  --config configfiles --header-files src/h --libs lib
  --main-class com.greetings.Main --man-pages man --module-version 1.0
  --os-arch "x86_x64" --os-name "Mac OS X"
  --os-version "10.10.5" greetingsmod 

EDIT: Expanded + clarified to have the answer that I was looking for.