1

I have an executable-jar that I can run with java -jar app.jar but the SDK is 326MB. This is a lot.

jlink can create a JRE, but I can't use jlink as I have a non-modular application.

Can you please tell me how to create a JRE?

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Dmitriy
  • 375
  • 1
  • 18
  • 1
    You have an JRE ... as you already mentioned your app is not modular so you can't use jlink to create a custom JRE... – khmarbaise Sep 24 '22 at 19:18
  • @khmarbaise so I have to use JDK instead of JRE, which weighs 326mb? I can't find JRE 18.0.1 – Dmitriy Sep 24 '22 at 19:25
  • 1
    You can try this: https://javaalmanac.io/ there are links to JRE's.. but reconsider upgrading to JDK19 because sind 20.09 JDK19 is the current release... – khmarbaise Sep 24 '22 at 19:34
  • @khmarbaise, thanks a lot! I found JRE 80mb there, with which my application started – Dmitriy Sep 24 '22 at 19:42
  • I’m voting to close this question because it is answered by comments – Queeg Sep 24 '22 at 20:46
  • 1
    “I can't use jlink as I have a non-modular application.” Why not write a module-info.java, then? As long as you’re not using illicit hacks of Java SE classes, it should work just fine. – VGR Sep 24 '22 at 20:59
  • @VGR I've been trying to make the app modular for over a month, but there have been an endless amount of bugs and incompatibilities since the libraries are big. I would be happy to have everything work by itself with the help of jlink. – Dmitriy Sep 24 '22 at 21:03
  • 1
    @VGR using module-info for creating a customized JRE is not enough. That means all of your deps must be modules as well and having jmod files as well. etc.. That's not the easy.. – khmarbaise Sep 24 '22 at 23:14
  • 1
    Perhaps [*jpackage*](https://docs.oracle.com/en/java/javase/19/jpackage/packaging-overview.html) is useful, I don't know. Console documentation [here](https://docs.oracle.com/en/java/javase/19/docs/specs/man/jpackage.html). – Basil Bourque Sep 24 '22 at 23:40

2 Answers2

4

jlink can be used to create a 'JRE'/runtime image for non-modular applications just fine. It just can't automatically derive the modules that should go in the runtime image in that case. They have to be specified manually instead.

For instance, if I have a simple app.jar:

$ java -jar app.jar
Hello World!

Then create a runtime image with jlink, with only the java.base module:

jlink --output runtime --add-modules java.base --strip-debug --no-header-files --no-man-pages

Then I can run the jar with the java executable in the runtime image:

$ ./runtime/bin/java -jar app.jar
Hello World!

And the runtime image is just ~35 MB (though this can vary per platform).


jdeps can be used to get an idea of which modules should be used to create the runtime image:

$ jdeps --print-module-deps add.jar
java.base

This will print a comma-separated list of modules that can be passed directly as an argument to the --add-modules option of jlink. (though, service interface implementations have to be handled separately. See e.g.: jdeps does not add jdk.random when using RandomGenerator.getDefault())

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

You can try to this as source:

https://javaalmanac.io/

where you can find links to download only JRE instead of JDK.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235