1 of 27

1

Java Packages and Modules

Link

rafabene@gmail.com

@rafabene - bit.ly/java-modules

2 of 27

rafabene@gmail.com

@rafabene

apiVersion: oracle/v1

kind: PrincipalSolutionArchitect

metadata:

name: Rafael Benevides

namespace: Oracle Linux team

annotations:

apache/contributor: Apache DeltaSpike PMC

labels:

developer: Java, NodeJS

hobby: 4x4, drones

spec:

replicas: 1

containers:

image: benevides/rafael:latest

Rafael Benevides

@rafabene - bit.ly/java-modules

3 of 27

@rafabene - bit.ly/java-modules

4 of 27

@rafabene - bit.ly/java-modules

5 of 27

@rafabene - bit.ly/java-modules

6 of 27

Java is moving fast...

Java 14 Released on March 17th 2020

@rafabene - bit.ly/java-modules

7 of 27

Each new release, made Java heavier

API: More features… mores features…

JVM: More features… more features….

Do we need?

  • Swing
  • CORBA
  • Applet
  • XML
  • RMI

@rafabene - bit.ly/java-modules

8 of 27

Project Jigsaw: Java Modules

  • The Modular JDK (Smaller JDKS)
  • Modular Source Code (Smaller JAR files)
  • Encapsulate Most Internal APIs
  • Java Platform Module System (Users can create their modules)
  • jlink: The Java Linker (Users can create their executable)

@rafabene - bit.ly/java-modules

9 of 27

First we need to understand Java packages

We need a way to organize ~4400 Java Classes and Interfaces by grouping them in (224) packages

@rafabene - bit.ly/java-modules

10 of 27

Packages: Folders in a file directory

Declare a class in a package:

package com.rafabene;

Import other packages in a class:

import java.util.*;

import java.util.List;

@rafabene - bit.ly/java-modules

11 of 27

Demo: Java packages

javac App.java -> App.class

Java uses fully-qualified name for a class:

java App

java com.rafabene.App

@rafabene - bit.ly/java-modules

12 of 27

Demo: Classpath

JVM is a "Virtual Machine" with mount-points:

Classpath = Classes root path

java -cp <PATH>

export CLASSPATH=<PATH>

@rafabene - bit.ly/java-modules

13 of 27

JPMS (Java Platform Module System)

  • Modularize the JDK itself.
  • Offer a module system for applications to use.
  • A class is a container of fields and methods
  • A package is a container of classes and interfaces
  • A module is a container of packages

@rafabene - bit.ly/java-modules

14 of 27

Problem: Classpath hell

Which Jars are used? Versions?

No difference between public/private classes

@rafabene - bit.ly/java-modules

15 of 27

Can I access class C?

  • Accessibility: Java 1-8
    • Containers: class, package
    • Class access: default (package), public

  • With modules: Java 9+
    • Containers: class, package, module
    • Class access: default (package), public, public within module, public to specific modules

Module M

Package P

Class A, B, C

@rafabene - bit.ly/java-modules

16 of 27

Packages need to be exported

Packages not exported can not be used

@rafabene - bit.ly/java-modules

17 of 27

Java module two-way

@rafabene - bit.ly/java-modules

18 of 27

Java module definition

module-info.java

module first {

requires java.sql;

exports com.rafabene.first;

exports com.rafabene.first.internal to friendmodule;

opens com.rafabene.first.internal;

}

@rafabene - bit.ly/java-modules

19 of 27

@rafabene - bit.ly/java-modules

20 of 27

Demo time

  • Reading module descriptors with jar -d
  • Restricting access to internal package in module 2
  • Allowing access to internal package to friend modules
  • Modules should not use cyclic dependency
  • Duplicated packages are not allowed
  • Running java application:

From: java -cp CLASSPATH CLASSNAME

To: java --module-path MODULEPATH -m MODULENAME/CLASSNAME

@rafabene - bit.ly/java-modules

21 of 27

Troubleshooting

java.lang.NoClassDefFoundError

  • Use jdeps to check your dependencies
  • Use --add-modules to add add a non java.se module

@rafabene - bit.ly/java-modules

22 of 27

jlink

Allows the creation of Runtime Images (Another name for JRE)

  • The user does not have to install the JRE
  • For ISVs, this can eliminate a host of potential support issues generated by not having the right version of the JDK that has been used to test and certify their software.

@rafabene - bit.ly/java-modules

23 of 27

Creating a custom Runtime image

  • java --list-modules

  • jlink --compress 2\

--strip-java-debug-attributes \

--no-header-files \

--no-man-pages \

--output minimal-jre \

--add-modules second \ (Dependencies will be included)

--launcher second=second/com.rafabene.second.App \

--module-path <MODULE_PATH>

@rafabene - bit.ly/java-modules

24 of 27

Creating Cloud-Native images with jlink and alpine

FROM adoptopenjdk as builder

WORKDIR /app

COPY first.jar .

COPY second.jar .

RUN jlink <with parameters>

FROM alpine:3.7-glibc2.25

WORKDIR /app

COPY --from=builder /app/dist/ ./

ENTRYPOINT ["bin/run"]

openjdk:11 464MB

app:latest 43MB

@rafabene - bit.ly/java-modules

25 of 27

Demo sources

rafabene/java-modules-demo

@rafabene - bit.ly/java-modules

26 of 27

The End

(but GraalVM is where you should look next)

@rafabene - bit.ly/java-modules

27 of 27

@rafabene

@RAFABENE

@rafabene - bit.ly/java-modules