JDK-9
Modules and Java Linker (JLink)
G. Bhanu Prakash
Java Platform Team
Oracle India Private Limited
bhanu.prakash.gopularam@oracle.com
8/30/2016 1Copyright 2016, Oracle and/or it's affiliates. All rights reserved
Agenda
1. Modules
2. Module Dependencies
3. Jlink and Packaging
4. Jlink Plugins
5. Example Plugins
1. System Module Descriptor Plugin
2. Compress Plugin
3. Release-Info Plugin
8/30/2016 2
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
SAFE HARBOR STATEMENT
The following is intended to outline our general product direction. It is
intended for information purpose only, and may not be incorporated in
any contract. It is not a commitment to deliver any material, code, or
functionality, and should not be relied upon in making purchasing
decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion
of Oracle
8/30/2016
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
3
Jigsaw Problems
Problems
 Platform scalability (small devices)
 Jar Hell or Classpath Hell
• Java runtime crashes with NoClassDefFoundError
• Shadowing of classes
 Performance
• Startup
• Download
8/30/2016 4
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Jigsaw Solutions
Problems
 Platform scalability
• Modularize JDK
 Jar Hell or Classpath Hell
• Replace classpath with module dependencies
 Performance
• Startup – install optimizations in module library
• Download - incremental modules in demand
8/30/2016 5
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
• What is Module
• Module-Info.java
– Visibility Rules
• REQUIRES
• EXPORTS
• REQUIRES PUBLIC
8/30/2016 6
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Modules
8/30/2016 7
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
“public” no longer means “accessible”
Modules
com.foo.app
module-info.java
module com.foo.app {
requires com.foo.bar;
requires java.sql;
}
8/30/2016 8
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Example: com.foo.app
Modules Example
com.foo.bar
module com.foo.app {
exports com.foo.bar;
}
Actual Module Dependencies (com.foo.app)
8/30/2016 9
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
8/30/2016 10
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Transitive Dependencies (com.foo.app)
JDK Platform Modules
8/30/2016 11
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
How to compile modules
Inputs
 Jmods, Modular jar files
 Exploded modules
1. Compile com.foo.bar (base)
javac -d modscom.foo.bar
srccom.foo.barmodule-info.java
srccom.foo.barcomfoobar*.java
2. Compile com.foo.app (main application)
javac -modulepath mods
-d modscom.foo.app
srccom.foo.appmodule-info.java
srccom.foo.appcomfooapp*.java
Or
javac –modulesourcepath src –d mods …
8/30/2016 12
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Create Modules
 Create module com.foo.bar
jmod create
--class-path modscom.foo.bar
com.foo.bar
 Create module com.foo.app
jmod create
--class-path modscom.foo.app
com.foo.app
8/30/2016 13
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Module Information
• jmod describe com.foo.bar
com.foo.bar
requires mandated java.base
exports com.foo.bar
• jmod describe com.foo.app
com.foo.app
requires com.foo.bar
requires mandated java.base
requires java.sql
conceals com.foo.app
8/30/2016 14
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
8/30/2016 15
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Linking
8/30/2016
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
16
Jlink and Packaging (JEP-282)
Brief History
 Jrecreate was introduced in Java 8 with EJDK (JavaSE Embedded)
 Instead of binaries we ship the EJDK
Jlink Usage: Command line tool to link module
 modules into jimage file
 generate runtime images
JEP-282: “Link time is an opportunity to do whole-world
optimizations that are otherwise difficult at compile or costly
at runtime”
8/30/2016 17
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Jlink Packaging
8/30/2016 18
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Legacy JDK image
JDK-9 Generated Image
Jlink Packaging
Create image file
jlink --output myimage
--addmods com.foo.app
--modulepath jdk-9b131jmods;mods
>ls myimage
bin lib conf release
8/30/2016
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
19
App execution from Generated image
>myimagebinjava -listmods
com.foo.app
com.foo.bar
java.base@9-ea
java.logging@9-ea
java.sql@9-ea
java.xml@9-ea
>java -m com.greetings/com.greetings.Main
Output: Greetings (com.foo.app.Main) : World (from com.foo.bar)
>java -m com.foo.app/com.foo.app.App2
Output: Exception:oracle.jdbc.driver.OracleDriver
8/30/2016 20
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
-modulepath mods
Not required
Jlink Plugins
 Image customization enabled using a set of predefined plugins
 12 builtin plugins. Example:
 compress-resources
 release-info
 sort-resources
 exclude-files
 gen-installed-modules
 replace-file
 Programmatic access to jlink features
 As per the JEP, the plugin API is strictly experimental
8/30/2016 21
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
8/30/2016 22
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Plugins and Image creation - WorkFlow
Optimizing Java startup
Problem: Significant time spent parsing and validating
module-info.class for each module in system image
itself
Solution: Implement jlink plugin to generate a pre-
validated system module graph
SystemModuleDescriptorPlugin
8/30/2016 23
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
--installed-modules Startup time
Disabled 510 ms
Enabled 220 ms
• Reduces memory use by a sizable amount
• This plugin is ON by default (--installed-modules on)
8/30/2016
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
24
Compress Plugin
LEVEL_0
StringSharingPlugin - Scans image classes constant pool (UTF-8 Strings)
LEVEL_1:
ZipPlugin(resFilter) - Zip compression of image classes
LEVEL_2:
StringSharingPlugin(resFilter)
ZipPlugin(resFilter)
8/30/2016 25
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Compression
level
Size (~)
None 28.5 MB
LEVEL_0 17.3 MB
LEVEL_1 11.7 MB
LEVEL_2 11.5 MB
Table: Jlink generated image size
Release-info Plugin
Normal JDK
myimage>cat release
#Wed Aug 24 09:19:38 IST 2016
MODULES=java.sql,com.foo.bar,com.foo.app,java.logging,java.xml,java.base
OS_VERSION=5.2
OS_ARCH=amd64
OS_NAME=Windows
JAVA_VERSION=9-ea
With Release Plugin:
jlink --release-info
add:build_type=fastdebug,source=oraclejdk,java_version=9+101 …
myimage2>cat release
#Wed Aug 24 09:32:36 IST 2016
MODULES=java.sql,com.foo.bar,com.foo.app,java.logging,java.xml,java.base
OS_VERSION=5.2
OS_ARCH=amd64
OS_NAME=Windows
JAVA_VERSION=9-ea
build_type=fastdebug,source=oraclejdk,java_version=9+101
8/30/2016 26
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Summary
• Accessibility is enforced by the compiler, VM, and Core
Reflection
• Freedom to adopt modules at your own pace
– Modularize the application
– Modularize the libraries
• Some libraries may require more changes to work as modules
8/30/2016
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
27
More Information
 OpenJDK Project Jigsaw page
 http://openjdk.java.net/project/jigsaw
 mailto: jigsaw-dev@openjdk.java.net
 Module Usage at Compile Time slides
http://openjdk.java.net/projects/jigsaw/doc/ModulesAndJavac.pdf
 Javadoc for Java Module java APIs:
http://cr.openjdk.java.net/~mr/jigsaw/api/
 Modules in the Java Language and VM
http://openjdk.java.net/projects/jigsaw/doc/lang-vm.html
8/30/2016 28
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
Thank You. Questions?
8/30/2016
Copyright 2016, Oracle and/or it's affiliates.
All rights reserved
29

JDK-9: Modules and Java Linker

  • 1.
    JDK-9 Modules and JavaLinker (JLink) G. Bhanu Prakash Java Platform Team Oracle India Private Limited bhanu.prakash.gopularam@oracle.com 8/30/2016 1Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 2.
    Agenda 1. Modules 2. ModuleDependencies 3. Jlink and Packaging 4. Jlink Plugins 5. Example Plugins 1. System Module Descriptor Plugin 2. Compress Plugin 3. Release-Info Plugin 8/30/2016 2 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 3.
    SAFE HARBOR STATEMENT Thefollowing is intended to outline our general product direction. It is intended for information purpose only, and may not be incorporated in any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle 8/30/2016 Copyright 2016, Oracle and/or it's affiliates. All rights reserved 3
  • 4.
    Jigsaw Problems Problems  Platformscalability (small devices)  Jar Hell or Classpath Hell • Java runtime crashes with NoClassDefFoundError • Shadowing of classes  Performance • Startup • Download 8/30/2016 4 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 5.
    Jigsaw Solutions Problems  Platformscalability • Modularize JDK  Jar Hell or Classpath Hell • Replace classpath with module dependencies  Performance • Startup – install optimizations in module library • Download - incremental modules in demand 8/30/2016 5 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 6.
    • What isModule • Module-Info.java – Visibility Rules • REQUIRES • EXPORTS • REQUIRES PUBLIC 8/30/2016 6 Copyright 2016, Oracle and/or it's affiliates. All rights reserved Modules
  • 7.
    8/30/2016 7 Copyright 2016,Oracle and/or it's affiliates. All rights reserved “public” no longer means “accessible” Modules
  • 8.
    com.foo.app module-info.java module com.foo.app { requirescom.foo.bar; requires java.sql; } 8/30/2016 8 Copyright 2016, Oracle and/or it's affiliates. All rights reserved Example: com.foo.app Modules Example com.foo.bar module com.foo.app { exports com.foo.bar; }
  • 9.
    Actual Module Dependencies(com.foo.app) 8/30/2016 9 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 10.
    8/30/2016 10 Copyright 2016,Oracle and/or it's affiliates. All rights reserved Transitive Dependencies (com.foo.app)
  • 11.
    JDK Platform Modules 8/30/201611 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 12.
    How to compilemodules Inputs  Jmods, Modular jar files  Exploded modules 1. Compile com.foo.bar (base) javac -d modscom.foo.bar srccom.foo.barmodule-info.java srccom.foo.barcomfoobar*.java 2. Compile com.foo.app (main application) javac -modulepath mods -d modscom.foo.app srccom.foo.appmodule-info.java srccom.foo.appcomfooapp*.java Or javac –modulesourcepath src –d mods … 8/30/2016 12 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 13.
    Create Modules  Createmodule com.foo.bar jmod create --class-path modscom.foo.bar com.foo.bar  Create module com.foo.app jmod create --class-path modscom.foo.app com.foo.app 8/30/2016 13 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 14.
    Module Information • jmoddescribe com.foo.bar com.foo.bar requires mandated java.base exports com.foo.bar • jmod describe com.foo.app com.foo.app requires com.foo.bar requires mandated java.base requires java.sql conceals com.foo.app 8/30/2016 14 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 15.
    8/30/2016 15 Copyright 2016,Oracle and/or it's affiliates. All rights reserved
  • 16.
    Linking 8/30/2016 Copyright 2016, Oracleand/or it's affiliates. All rights reserved 16
  • 17.
    Jlink and Packaging(JEP-282) Brief History  Jrecreate was introduced in Java 8 with EJDK (JavaSE Embedded)  Instead of binaries we ship the EJDK Jlink Usage: Command line tool to link module  modules into jimage file  generate runtime images JEP-282: “Link time is an opportunity to do whole-world optimizations that are otherwise difficult at compile or costly at runtime” 8/30/2016 17 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 18.
    Jlink Packaging 8/30/2016 18 Copyright2016, Oracle and/or it's affiliates. All rights reserved Legacy JDK image JDK-9 Generated Image
  • 19.
    Jlink Packaging Create imagefile jlink --output myimage --addmods com.foo.app --modulepath jdk-9b131jmods;mods >ls myimage bin lib conf release 8/30/2016 Copyright 2016, Oracle and/or it's affiliates. All rights reserved 19
  • 20.
    App execution fromGenerated image >myimagebinjava -listmods com.foo.app com.foo.bar java.base@9-ea java.logging@9-ea java.sql@9-ea java.xml@9-ea >java -m com.greetings/com.greetings.Main Output: Greetings (com.foo.app.Main) : World (from com.foo.bar) >java -m com.foo.app/com.foo.app.App2 Output: Exception:oracle.jdbc.driver.OracleDriver 8/30/2016 20 Copyright 2016, Oracle and/or it's affiliates. All rights reserved -modulepath mods Not required
  • 21.
    Jlink Plugins  Imagecustomization enabled using a set of predefined plugins  12 builtin plugins. Example:  compress-resources  release-info  sort-resources  exclude-files  gen-installed-modules  replace-file  Programmatic access to jlink features  As per the JEP, the plugin API is strictly experimental 8/30/2016 21 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 22.
    8/30/2016 22 Copyright 2016,Oracle and/or it's affiliates. All rights reserved Plugins and Image creation - WorkFlow
  • 23.
    Optimizing Java startup Problem:Significant time spent parsing and validating module-info.class for each module in system image itself Solution: Implement jlink plugin to generate a pre- validated system module graph SystemModuleDescriptorPlugin 8/30/2016 23 Copyright 2016, Oracle and/or it's affiliates. All rights reserved --installed-modules Startup time Disabled 510 ms Enabled 220 ms
  • 24.
    • Reduces memoryuse by a sizable amount • This plugin is ON by default (--installed-modules on) 8/30/2016 Copyright 2016, Oracle and/or it's affiliates. All rights reserved 24
  • 25.
    Compress Plugin LEVEL_0 StringSharingPlugin -Scans image classes constant pool (UTF-8 Strings) LEVEL_1: ZipPlugin(resFilter) - Zip compression of image classes LEVEL_2: StringSharingPlugin(resFilter) ZipPlugin(resFilter) 8/30/2016 25 Copyright 2016, Oracle and/or it's affiliates. All rights reserved Compression level Size (~) None 28.5 MB LEVEL_0 17.3 MB LEVEL_1 11.7 MB LEVEL_2 11.5 MB Table: Jlink generated image size
  • 26.
    Release-info Plugin Normal JDK myimage>catrelease #Wed Aug 24 09:19:38 IST 2016 MODULES=java.sql,com.foo.bar,com.foo.app,java.logging,java.xml,java.base OS_VERSION=5.2 OS_ARCH=amd64 OS_NAME=Windows JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101 … myimage2>cat release #Wed Aug 24 09:32:36 IST 2016 MODULES=java.sql,com.foo.bar,com.foo.app,java.logging,java.xml,java.base OS_VERSION=5.2 OS_ARCH=amd64 OS_NAME=Windows JAVA_VERSION=9-ea build_type=fastdebug,source=oraclejdk,java_version=9+101 8/30/2016 26 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 27.
    Summary • Accessibility isenforced by the compiler, VM, and Core Reflection • Freedom to adopt modules at your own pace – Modularize the application – Modularize the libraries • Some libraries may require more changes to work as modules 8/30/2016 Copyright 2016, Oracle and/or it's affiliates. All rights reserved 27
  • 28.
    More Information  OpenJDKProject Jigsaw page  http://openjdk.java.net/project/jigsaw  mailto: jigsaw-dev@openjdk.java.net  Module Usage at Compile Time slides http://openjdk.java.net/projects/jigsaw/doc/ModulesAndJavac.pdf  Javadoc for Java Module java APIs: http://cr.openjdk.java.net/~mr/jigsaw/api/  Modules in the Java Language and VM http://openjdk.java.net/projects/jigsaw/doc/lang-vm.html 8/30/2016 28 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
  • 29.
    Thank You. Questions? 8/30/2016 Copyright2016, Oracle and/or it's affiliates. All rights reserved 29