JDK-9: Modules and Java Linker

29
JDK-9 Modules and Java Linker (JLink) G. Bhanu Prakash Java Platform Team Oracle India Private Limited [email protected] 8/30/2016 1 Copyright 2016, Oracle and/or it's affiliates. All rights reserved

Transcript of JDK-9: Modules and Java Linker

Page 1: JDK-9: Modules and Java Linker

JDK-9 Modules and Java Linker (JLink)

G. Bhanu Prakash

Java Platform Team

Oracle India Private Limited

[email protected]

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

Page 2: JDK-9: Modules and Java Linker

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

Page 3: JDK-9: Modules and Java Linker

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

Page 4: JDK-9: Modules and Java Linker

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

Page 5: JDK-9: Modules and Java Linker

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

Page 6: JDK-9: Modules and Java Linker

• 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

Page 7: JDK-9: Modules and Java Linker

8/30/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

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; }

Page 9: JDK-9: Modules and Java Linker

Actual Module Dependencies (com.foo.app)

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

All rights reserved

Page 10: JDK-9: Modules and Java Linker

8/30/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

JDK Platform Modules

8/30/2016 11 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 12: JDK-9: Modules and Java Linker

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 …

8/30/2016 12

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

Page 13: JDK-9: Modules and Java Linker

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

8/30/2016 13 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 14: JDK-9: Modules and Java Linker

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

Page 15: JDK-9: Modules and Java Linker

8/30/2016 15 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 16: JDK-9: Modules and Java Linker

Linking

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

All rights reserved 16

Page 17: JDK-9: Modules and Java Linker

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

Page 18: JDK-9: Modules and Java Linker

Jlink Packaging

8/30/2016 18 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Legacy JDK image

JDK-9 Generated Image

Page 19: JDK-9: Modules and Java Linker

Jlink Packaging

Create image file

jlink --output myimage

--addmods com.foo.app

--modulepath jdk-9b131\jmods;mods

>ls myimage

bin lib conf release

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

All rights reserved 19

Page 20: JDK-9: Modules and Java Linker

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

Page 21: JDK-9: Modules and Java Linker

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

Page 22: JDK-9: Modules and Java Linker

8/30/2016 22 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Plugins and Image creation - WorkFlow

Page 23: JDK-9: Modules and Java Linker

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

Page 24: JDK-9: Modules and Java Linker

• 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

Page 25: JDK-9: Modules and Java Linker

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

Page 26: JDK-9: Modules and Java Linker

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

Page 27: JDK-9: Modules and Java Linker

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

Page 28: JDK-9: Modules and Java Linker

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

8/30/2016 28 Copyright 2016, Oracle and/or it's affiliates.

All rights reserved

Page 29: JDK-9: Modules and Java Linker

Thank You. Questions?

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

All rights reserved 29