11

I am a little bit confused about jlink. I am able to compile my source files and then use jlink with the following command

jlink -p $JAVA_HOME/jmods:mymods:<a few jars separated by colon here> \
      --add-modules com.myModuleApp \
      --output outfolder \
      --launcher launch=com.myModuleApp/com.myModuleApp.Main

it returns a tree structure folder like this:

outfolder
├── bin
│   └── launch
├── conf
├── include
├── legal
├── lib
└── release

On jlink --help there is no mention of how to include resources using any of its options.

I am able to run the launcher launch by double clicking it but then I get errors related to not being able to find the resources (naturally, as they are not in the outfolder).

However, even if I try to put the resources manually anywhere and everywhere in the directory tree, the launcher still is not able find the resources.

Is jlink designed to replace javapackager completely, partially, at all?

Can we deploy an app by jlinking the modules in a Java 9 app and then just bundling it in a jar?

If we use jlink, how can we properly include the resources files in the outfolder?

~ thanks in advance ~

saqe hi
  • 181
  • 2
  • 8
  • 2
    It would be useful if you could expand on what you mean by "resources"? For starters, I would expect the resources in your module to be in your module JAR (on the module path that you specify to jlink). Code in your module will be able to locate these via Class.getResourceXXX, exactly the same as when you run with the modular JAR on the module path. – Alan Bateman Jan 31 '18 at 07:22
  • hello, by `resources` I mean `.txt` files, Image icons for example, between others. – saqe hi Jan 31 '18 at 07:27
  • I understand [a module is, basically, the src files plus its data i.e.resources](https://medium.com/@chandra25ms/java9-modularity-part1-a102d85e9676). So, indeed I include resources within each module respectively as they are used. `jlink` does not produce a `jar` btw – saqe hi Jan 31 '18 at 07:40
  • 1
    If your module (be it packaged as a modular JAR or exploded on the file system) includes resources then jlink will include then in the run-time image. Is it possible that you've put the resources in a different directory tree so that they are not in the module specified to jlink? – Alan Bateman Jan 31 '18 at 07:43
  • the resource file, for example a `.txt` file, which is loaded by the module `com.myModuleApp` is at different directory levels as I am trying to solve this issue *(and understand better).* Running on `IntelliJ` works fine and I am not using `classpath`s. When I run `jlink` it does not save the resource into the exploded directory. **Note I use the command as shown in the question description** – saqe hi Jan 31 '18 at 07:54
  • Can you paste in the Class.getResourceXXX code that you use to locate the resource? If it works on the module path then it should work in the run-time image you create with jlink. – Alan Bateman Jan 31 '18 at 08:03
  • yes, `AboutDialog.class.getResourceAsStream("hello.txt")` – saqe hi Jan 31 '18 at 08:06
  • 2
    If hello.txt is in the same directory AboutDialog.class then it should work on either the module path or in the run-time image created by jlink. If you are saying that it's in a different directory tree and it works in IntelliJ then IntelliJ must be using `--patch-module` to augment the module with the resources in the other tree. – Alan Bateman Jan 31 '18 at 08:13
  • Actually, it is in the same directory as the `AboutDialog.class` so it finds it no problem there. The issue is that `jlink` does not copy it over in the exploded `outfolder` – saqe hi Jan 31 '18 at 08:17
  • 3
    jlink does copy resources. You can use `jimage list outfolder/lib/modules` to list all the resources in the generated run-time image and I would expect to see hello.txt listed under your module name. – Alan Bateman Jan 31 '18 at 08:27
  • 1
    `jimage` is very helpful. I can verify that `hello.txt` was not in the module. However, I looked into `mymods` which are just the `.class` compiled from `myModuleApp` and `hello.txt` is not there either. So, I put the `helllo.txt` resource file to the same directory where `AboutDialog.class` is located. Now, I can corroborate that `jlink`, effectively, copies the resources , in this case `hello.txt`, in the module thanks @AlanBateman – saqe hi Jan 31 '18 at 08:58
  • 2
    Thanks for confirming. One thing that isn't clear is how this works in IntelliJ. Can you see if the IDE is sneaking in a --patch-module option to patch the module with the resources in the other directory tree? I can't otherwise explain how it could work in the IDE. Also can you confirm that you tested the module on the module path before trying jlink - I'll bet you would have found the issue there first. – Alan Bateman Jan 31 '18 at 11:19

0 Answers0