Top Banner
17

Java 9 overview

Mar 15, 2018

Download

Software

Michel Schudel
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java 9 overview
Page 2: Java 9 overview

Java 9 goes agile

• Java 9: September 2017

• Java 10: March 2018 (18.3)

• Java 11: September 2018 (18.9)

….

Each 3rd release will be a long-term-support release (LTS) starting with Java 8!

Page 3: Java 9 overview

Some changes…

Page 4: Java 9 overview

Java 9 Highlights demo

1. Read–eval–print loop (REPL) environment: JShell

2. Language and API highlights

3. Modules and linking

Page 5: Java 9 overview

Modules

Page 6: Java 9 overview

The problem

•No real encapsulation at component / jar level

•Classpath hell / shadowing

•Classes used that shouldn’t be used •sun.internal.*;

•No way to select stuff from the JRE

Page 7: Java 9 overview

The solution: modules

•Module is a JAR with a module-info.java file in the root

• In the module-info.java, explicitly state which packages are “public”

Classes in the JDK have also been grouped into modules (java.base, java.util.logging, etc)

Page 8: Java 9 overview
Page 9: Java 9 overview

module-info.java

module mymodule {

requires java.sql; <- module name

requires commons.collections; <-module name

exports nl.craftsmen.mypackage; <-package

}

You don’t need to specify java.base, it is always needed

Page 10: Java 9 overview

FOO BAR

ApacheCommons

lang3

Java util logging

Page 11: Java 9 overview

Compiling with modules

• Modules go on the module path instead of the class path• javac –-module-path othermodule.jar myclass

• java –jar mymodule.jar --module-path othermodule.jar

• It is not allowed to have two modules on the classpath that export the same package to the same module

Page 12: Java 9 overview

Backward compatibility

• Modular JARs (with module-info.java) just run as normal JARs on Java8

• Non-modular JARs are considered “automatic modules” in when on the module path in Java9• Export all of their packages

• Requires all other modules

• All jars on the classpath go into the “unnamed” module• Export all of their packages

• Requires all other modules

• You don’t need to specify a “requires” to access it

Page 13: Java 9 overview
Page 14: Java 9 overview

Service loaders

Page 15: Java 9 overview

Service loaders

module interfacemodule {

exports nl.craftsmen.interfacemodule.ServiceInterface;

}

module consumermodule {

requires interfacemodule;

uses nl.craftsmen.interfacemodule.ServiceInterface;

}

module providermodule {

requires interfacemodule;

provides nl.craftsmen.interfacemodule.ServiceInterface

with nl.craftsmen.providermodule.ServiceInterfaceImpl;

}

Page 16: Java 9 overview

Service loaders

ServiceLoader<ServiceInterface> loader =

ServiceLoader.load(ServiceInterface.class);

for (ServiceInterface interface : loader) {

interface.doSomething();

}

Page 17: Java 9 overview