The Java Platform Module System
Java 9 released in 2017 JPMS is one of important feature of java 9.
What is a module?
A module has a name, it groups related code and is self-contained.
Java 9 Module System has a “java.base” Module. It’s known as Base Module. It’s an Independent module and does NOT dependent on any other modules. By default, all other modules dependent on “java.base”.
In java 9, modules helps you in encapsulating packages and manage dependencies. So typically,
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
A module is typically just a jar file that has a module-info.class file at the root.
To use a module, include the jar file into modulepath instead of the classpath. A modular jar file added to classpath is normal jar file and module-info.class file will be ignored.
How to find jdk modules in java 9
go to command prompt / shell where java 9 installed and run commands
$java --list-modules
this will display all modules in jdk.
$java --list-modueles | grep "java\."
// this will list modules starting with java.
$java --describe-module java.sql
output:
exports java.sql
exports javax.sql
exports javax.transaction.xa
requires java.logging transitive
requires java.base mandated
requires java.xml transitive
uses java.sql.Driver
using jdeps perform dependency analysis on main file of old project
$jdeps -jdkinternals Main.class
............
.....
...
it will suggest suggested replacement for jdk internal API.
Using Non-default Modules, adding modules to java 8 project:
$javac --add-modules java.xml.bind Main.java
$java --add-modules java.xml.bind Main
Java 9 released in 2017 JPMS is one of important feature of java 9.
What is a module?
A module has a name, it groups related code and is self-contained.
Java 9 Module System has a “java.base” Module. It’s known as Base Module. It’s an Independent module and does NOT dependent on any other modules. By default, all other modules dependent on “java.base”.
In java 9, modules helps you in encapsulating packages and manage dependencies. So typically,
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
A module is typically just a jar file that has a module-info.class file at the root.
To use a module, include the jar file into modulepath instead of the classpath. A modular jar file added to classpath is normal jar file and module-info.class file will be ignored.
module-info.java module java.base { exports java.lang; exports java.util; exports java.io; // and more } module-info.java module java.sql { exports java.sql; exports javax.sql; exports javax.transaction.xa; requires java.logging; requires java.xml; }
How to find jdk modules in java 9
go to command prompt / shell where java 9 installed and run commands
$java --list-modules
this will display all modules in jdk.
$java --list-modueles | grep "java\."
// this will list modules starting with java.
$java --describe-module java.sql
output:
exports java.sql
exports javax.sql
exports javax.transaction.xa
requires java.logging transitive
requires java.base mandated
requires java.xml transitive
uses java.sql.Driver
using jdeps perform dependency analysis on main file of old project
$jdeps -jdkinternals Main.class
............
.....
...
it will suggest suggested replacement for jdk internal API.
Using Non-default Modules, adding modules to java 8 project:
$javac --add-modules java.xml.bind Main.java
$java --add-modules java.xml.bind Main
No comments:
Post a Comment