Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run images generated by JDK 9 jlink?

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.

like image 251
clay Avatar asked Sep 25 '17 20:09

clay


People also ask

Which set of commands is necessary to create and run a custom runtime image from Java source files?

To create a custom Java runtime image for your application, run jlink before you package your application. Then pass the image produced to the packaging tool using the --runtime-image option.

How do I create a custom JRE?

You can use jdep command on your custom module to know module dependency . 5. Now use jlink command line tool to create your custom JRE. Command will create new JRE and now you can use your own JRE to run your program.

What is a JMOD file?

The jmod tool is intended for modules that have native libraries or other configuration files or for modules that you intend to link, with the jlink tool, to a runtime image. The JMOD file format let's you aggregate files other than . class files, metadata, and resources.

What is JLINK in Java 9?

Java 9 introduced jlink command-line tool which assembles and optimizes the specified modules and their dependencies into a custom runtime image. In other words it assembles a Java application and its dependent modules (instead of all modules which come with default JDK) into a custom JRE.

How does JLINK work with jpackage?

jpackage will run jlink and generate a runtime image, or you can bring your own image that you created yourself with jlink. To learn more about how easy the jpackage tool is check out this great talk on YouTube Java Packaging Tool: Create Native Packages to Deploy Java Applications by Kevin Rushforth.

How to use JLINK in a custom JRE?

In order to use jlink, we need to know the list of the JDK modules that the application uses and that we should include in our custom JRE. Let's use the jdeps command to get the dependent modules used in the application:

What's new in JDK 9?

At the moment, JDK 9 is only available as Early Access (EA) to let the community take a look how it works and what can be improved. Apart from all the news, for example about the modular system Jigsaw, there is one important question: How can I create a Java runtime with Maven? But before we can begin, let's do a quick recap of runtime images.


Video Answer


1 Answers

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.

like image 194
Naman Avatar answered Oct 09 '22 16:10

Naman