7

I am creating a sample application with 3 modules user, dept and account. In my user module, I have a main class and compile my modules with the following command:

javac -d target --module-source-path src $(find -name "*.java")

After compiling, execute following command for run:

java -p target -m com.user/com.user.info.Launcher

The output after running java modules are successful. But when trying to create runtime image using jlink the image created successfully but module executable script is not there. For create an image, I am using the following command:

jlink --module-path $JAVA_HOME/jmods:target --add-modules com.user --output my-app

In, runtime image, I have bin directory, but this directory contains only java and keynote script. I am expecting user main class script as well, for executing my application.

My Java version as below:

java version "9-ea"
Java(TM) SE Runtime Environment (build 9-ea+165)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+165, mixed mode)

How can I resolve this problem?

Naman
  • 27,789
  • 26
  • 218
  • 353
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
  • Afaik `jlink` does not create an executable. It just creates a runtime image of a VM that only has the modules you need. – Jorn Vernee May 20 '17 at 12:11
  • But in the greeting sample that mention in open jdk, have the script of greeting – Harmeet Singh Taara May 20 '17 at 12:14
  • Can you link the place where you see that? I don't see it on here: http://openjdk.java.net/projects/jigsaw/quick-start – Jorn Vernee May 20 '17 at 12:16
  • Yes this is not there, I think I was reading in Java 9 modularity book by http://googleweblight.com/i?u=http://shop.oreilly.com/product/mobile/0636920049494.do&grqid=q1IU6bWs&hl=en-IN&geid. Actually I am using lots of resources for reading, that's why, I was little bit confused – Harmeet Singh Taara May 20 '17 at 12:20
  • 2
    Still, the thing is, if this image contains all required module, then why we are not directly launch our application from image? If not, how can we use image for our application? – Harmeet Singh Taara May 20 '17 at 12:21
  • Your own module is included in the image. With the `java` binary in the `bin` folder, you can use `java com.user.info.Launcher` to launch the application. – Jorn Vernee May 20 '17 at 12:27
  • Superb feature, thanks @Jorn for your help. Please post your answer, so I can accept. – Harmeet Singh Taara May 20 '17 at 12:31
  • Glad I could help :) – Jorn Vernee May 20 '17 at 12:38

1 Answers1

13

jlink creates a runtime VM image in which it includes only the modules that are needed.

Since you specified --add-modules com.user the image will include the com.user module, and all of the modules it (directly or indirectly) depends on.

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

java com.user.info.Launcher

You can also have jlink generate a launcher script using the --launcher <command>=<module>/<main> option. In your case you could do something like:

jlink --module-path $JAVA_HOME/jmods:target --add-modules com.user --output my-app --launcher launch=com.user/com.user.info.Launcher

And after that, you can just use launch from the bin directory to run the application.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93