How do I run images generated by JDK 9 jlink?
Asked Answered
S

1

18

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.

Strap answered 25/9, 2017 at 20:3 Comment(1)
I answered this one a while ago: stackoverflow.com/questions/44085367Polinski
D
15

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.

Davies answered 25/9, 2017 at 20:14 Comment(4)
That first command doesn't use jlink or the output generated by jlink at all. That uses the system Java and the compiled .class files in the mods directory.Strap
@Strap correct . that's what I had mentioned as well from the module structure...updated it to make it clear.Davies
an extra question would be: how do you debug the custom image? The -Xdebug doesn't workDeglutinate
If someone build with a launcher script like above. the run command should be greetingsapp/bin/launch instead of greetingsapp/bin/launcherJaninajanine

© 2022 - 2024 — McMap. All rights reserved.