17

Java 9 overview

Embed Size (px)

Citation preview

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!

Some changes…

Java 9 Highlights demo

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

2. Language and API highlights

3. Modules and linking

Modules

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

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)

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

FOO BAR

ApacheCommons

lang3

Java util logging

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

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

Service loaders

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;

}

Service loaders

ServiceLoader<ServiceInterface> loader =

ServiceLoader.load(ServiceInterface.class);

for (ServiceInterface interface : loader) {

interface.doSomething();

}