+ All Categories
Home > Documents > JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf ·...

JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf ·...

Date post: 14-Aug-2020
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
31
JDK-9 Modules and Java Linker (JLink) G. Bhanu Prakash Java Platform Team Oracle India Private Limited [email protected] 9/26/2016 1 Copyright 2016, Oracle and/or it's affiliates. All rights reserved
Transcript
Page 1: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

JDK-9 Modules and Java Linker (JLink)

G. Bhanu Prakash

Java Platform Team

Oracle India Private Limited

[email protected]

9/26/2016 1 Copyright 2016, Oracle and/or it's affiliates. All rights reserved

Page 2: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

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

4. Class.forName Plugin

9/26/2016 2 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 3: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

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

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 3

Page 4: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Jigsaw Problems Problems

Platform scalability (small devices)

Jar Hell or Classpath Hell

• Java runtime crashes with NoClassDefFoundError

• Shadowing of classes

Performance

• Startup

• Download

9/26/2016 4 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 5: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

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

9/26/2016 5 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 6: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

• What is Module

• Module-Info.java – Visibility Rules

• REQUIRES

• EXPORTS

• REQUIRES PUBLIC

9/26/2016 6 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Modules

Page 7: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

9/26/2016 7 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

“public” no longer means “accessible”

Modules

Page 8: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

com.foo.app module com.foo.app { requires com.foo.bar; requires java.sql; }

9/26/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; }

Page 9: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Actual Module Dependencies (com.foo.app)

9/26/2016 9 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 10: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

9/26/2016 10 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Transitive Dependencies (com.foo.app)

Page 11: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

JDK Platform Modules

9/26/2016 11 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 12: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

How to compile modules

Inputs Jmods, Modular jar files

Exploded modules

1. Compile com.foo.bar (base)

javac -d mods\com.foo.bar

src\com.foo.bar\module-info.java

src\com.foo.bar\com\foo\bar\*.java

2. Compile com.foo.app (main application)

javac -modulepath mods

-d mods\com.foo.app

src\com.foo.app\module-info.java

src\com.foo.app\com\foo\app\*.java

Or

javac –modulesourcepath src –d mods …

9/26/2016 12

Copyright 2016, Oracle and/or it's affiliates. All rights reserved

Page 13: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Create Modules

Create module com.foo.bar

jmod create

--class-path mods\com.foo.bar

com.foo.bar

Create module com.foo.app jmod create

--class-path mods\com.foo.app

com.foo.app

9/26/2016 13 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 14: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

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

9/26/2016 14 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 15: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

9/26/2016 15 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 16: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Linking

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 16

Page 17: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

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”

9/26/2016 17

Copyright 2016, Oracle and/or it's affiliates. All rights reserved

Page 18: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Jlink Packaging

9/26/2016 18 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Legacy JDK image

JDK-9 Generated Image

JDK

Page 19: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Jlink Packaging

Create image file

jlink --output myimage

--addmods com.foo.app

--modulepath jdk-9b131\jmods;mods

>ls myimage

bin lib conf release

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 19

Page 20: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

App execution from Generated image

>myimage\bin\java -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.foo.app/com.foo.app.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

9/26/2016 20 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

--modulepath option Not required

Page 21: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Jlink Plugins

Image customization enabled using a set of predefined plugins

12 builtin plugins. Example: compress-resources

release-info

exclude-files

gen-installed-modules

replace-file

Programmatic access to jlink features

As per the JEP, the plugin API is strictly experimental

9/26/2016 21 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 22: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Plugin Internals @see jdk/tools/jlink/plugin/Plugin.java (link) Plugin categories: FILTER("FILTER“) TRANSFORMER("TRANSFORMER"), 56 MODULEINFO_TRANSFORMER("MODULEINFO_TRANSFORMER") COMPRESSOR("COMPRESSOR“) PACKAGER("PACKAGER") Plugin state: public enum State {DISABLED, AUTO_ENABLED, FUNCTIONAL} Methods: public default void configure(Map<String, String> config) public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out)

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 22

Page 23: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

9/26/2016 23 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Plugins and Image creation - WorkFlow

Plugin Types 1. TransformerPlugin 2. PostProcessorPlugin

Page 24: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

1. Optimizing Java startup (SystemModuleDescriptorPlugin)

Problem: Significant time spent parsing and validating

module-info.class for each module in system image Solution: Generate a pre-validated system module graph De-duplicating identical set of export targets Time: java -version

9/26/2016 24 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

--installed-modules Startup time

Disabled 510 ms

Enabled 220 ms

Page 25: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

• Reduces memory use by a sizable amount

• This plugin is ON by default (--installed-modules on)

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 25

Page 26: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

2. 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)

9/26/2016 26 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

Page 27: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

3. 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

9/26/2016 27 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 28: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

4. Class.forName Plugin

• Converts class.forName calls to constant loads

• Transform method overridden

– Scans all class files for class.forName()

– Removes current LdcInsnNode

– Sets new LdcInsnNode with proper reference object information

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 28

Page 29: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

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

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 29

Page 30: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

More Information

OpenJDK Project Jigsaw page

http://openjdk.java.net/project/jigsaw

mailto: [email protected]

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

9/26/2016 30 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 31: JDK-9 Modules and Java Linker (JLink) - Meetupfiles.meetup.com/3189882/BoJUG-Bhanu.pdf · JAVA_VERSION=9-ea With Release Plugin: jlink --release-info add:build_type=fastdebug,source=oraclejdk,java_version=9+101

Thank You. Questions?

9/26/2016 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved 31


Recommended