Limit modules added by javapackager

泄露秘密 提交于 2019-12-03 13:01:50

So it turns out that the reason why it wasn't honoring my options is because my app jar is not a Java 9 module. It's a plain old jar. In Java 9, javapackager uses jlink to generate the runtime and will not try to limit the number of modules because it can't determine the module dependencies. The -Bruntime option is only for Java Web Start applications. If you want javapackager to not use jlink, you have to use the one in JDK 8.

I cannot turn my app jar into a module due to the complexity involved with all the 3rd party dependencies (you can see the details in this question). And I can't use JDK 8 because I need some APIs added to the Desktop module in Java 9. So, I found a workaround to get a slimmer runtime into the app using the Java 9 javapackager:

  1. Generate a slimmer runtime using jlink by specifying only the modules that you need using the --add-modules option. You can use the jdeps command to figure out which modules are needed by all the jars in your app.
  2. Generate the .app with the full runtime using javapackger as usual.
  3. Open up your generated .app file (macOS) or for Windows, use something that will let you edit the .exe or .msi installer (don't know how to do this myself).
  4. On macOS, replace the contents of <myApp>.app/Contents/PlugIns/Java.runtime/Contents/Home with those of your slimmer runtime. Do something similar for the Windows installer.

Using the command line javapackager, you can make use of the deploy options --limit-modules and --add-modules such as:-

javapackager -deploy 
    --add-modules java.base,java.desktop... 
    --limit-modules java.base,java.desktop,java.naming...
    --module-path your.mods.dir
    -native -outdir OUTPUT_DIR -outfile APPLICATION_NAME 
    -srcdir PACKAGE_SRC_DIR -srcfiles APPLICATION.jar -appclass MAIN_CLASS
    -name "YourApplication" -title "SelfContained"

Or colloquially a deploy ant task would consist of something like:-

<fx:runtime strip-native-commands="false">
    <fx:add-modules value="java.base"/>
    <fx:add-modules value="java.desktop,java.naming..."/>
    <fx:limit-modules value="java.base"/>
    <fx:limit-modules value="java.desktop,java.naming..."/>
    <fx:module-path value="${java.home}/../images/jmods"/>
    <fx:module-path value="${build.dir/modules"/>
</fx:runtime>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!