Skip to content

bennofs/jpms-examples

Repository files navigation

Examples for the Java Platform Module System (JPMS)

This is some example code to test features of the java module system.

Modules

  • hello: simple module with main class example.Hello
  • hello.conflict: copy of hello, with different module name
  • lib: simple library module, providing class lib.Example
  • lib.main: module with main class depending on lib module
  • lib.reflectmain: main class that reflectively loads lib.Example
  • lib.cpmain: non-modular main class that is compiled against lib.Example on the classpath
  • libconflict: copy of lib with different module name
  • conflict.uselib: library that requires lib
  • conflict.main: module with main class requiring both conflict.uselib and libconflict

Examples

Running a simple hello world module:

$ java --module-path $PWD/classes -m hello/example.Hello
hello world!
module java.prefs
module jdk.charsets
...
module hello
module jdk.compiler
...

Adding modules to the boot module layer:

$ java --module-path $PWD/classes --add-modules lib -m hello/example.Hello 
...
module lib
...
module hello
...

A runtime conflict due to the same package being present in multiple modules (each ModuleLayer, including the boot module layer, enforces that a package is defined in at most one module):

$ java --module-path $PWD/classes -m conflict.main/main.Main
Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package lib in both module lib and module libconflict

Non-modular applications can access modules in two different ways. First, we can place the module on the classpath (then the module-info.class is ignored and the classes just get added to the unnamed module):

# modules can be placed on the classpath (then their module-info.class is ignored)
$ java -classpath classes/lib.cpmain:classes/lib main.Main
value: 4
classloader equal: true
main module: unnamed module @198e2867
lib module: unnamed module @198e2867

The second way is to explictly or implictly load the module:

# explictly load the module
$ java -classpath classes/lib.cpmain --module-path classes/ --add-modules lib main.Main
value: 4
classloader equal: true
main module: unnamed module @90f6bfd
lib module: module lib

# load the module by way of transitive requirements (conflict.uselib requires lib)
$ java -classpath classes/lib.cpmain --module-path classes/ --add-modules conflict.uselib main.
Main
value: 4
classloader equal: true
main module: unnamed module @90f6bfd
lib module: module lib

If both are combined, the module takes precendence over the classpath:

# lib.Example from libconflict is used, even though classes/lib is also on the classpath
$ java -classpath classes/lib.cpmain:classes/lib --module-path classes/ --add-modules libconfli
ct main.Main
value: 1
classloader equal: true
main module: unnamed module @4157f54e
lib module: module libconflict

Reflective access behaves the same: either there is already a loaded module providing the class, or the class is looked up in the classpath:

# taken from classpath
$ java -cp classes/lib --module-path $PWD/classes -m lib.reflectmain/main.Main
value: 4
classloader equal: true
main module: module lib.reflectmain
lib module: unnamed module @38082d64
# taken from already-loaded module
$ java -cp classes/lib --module-path $PWD/classes --add-modules libconflict -m lib.reflectmain/
main.Main
value: 1
classloader equal: true
main module: module lib.reflectmain
lib module: module libconflict

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published