Java 9 Modules: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 32: Line 32:


All the code from classpath lives together in the "unnamed" module.
All the code from classpath lives together in the "unnamed" module.
==<tt>module-info.java</tt>==
<code>module-info.java</code> is file that represents a module descriptor.
If code is compiled without a <code>module-info.java</code>, the code will be part of the "unnamed" module and can see all other code in the "unnamed", <code>java.base</code> and modules in the <code>java.se</code> root module. That means if no <code>module-info.java</code> is present in the project, everything should still work as in Java 8. Dependencies should be put on the [[#Classpath|classpath]], not on [[#Modulepath|modulepath]].
If the code is compiled with a <code>module-info.java</code>, the code will become part of its own named module, and can only see code in <code>java.base</code> and other named modules with are referenced with <code>requires</code> in <code>module-info.java</code>.


=Organizatorium=
=Organizatorium=
* Classpath and module path is mutually exclusive.
* Classpath and module path is mutually exclusive.

Revision as of 21:25, 18 May 2021

External

Internal

Concepts

Module

A module is a group of closely related Java packages and resources, shipped together with a module descriptor file module-info.java. A module can be thought of as a package of Java packages. Modules are exposed to the JVM on the module path.

Module Types

Named Module

Unnamed Module

The "unnamed" module contains code compiled without a module-info.java descriptor.

Automatic Module

For JARs created before Java 9, code will get a module name derived from the JAR file name.

Package

A module package is identical with a regular Java package. While writing a module, code is organized internally in packages just like before Java 9. Packages are used to determine what code is publicly accessible outside the module.

Resource

Each module encapsulates its resources like configuration files and media.

Modulepath

All the code on the modulepath lives in their own "named" modules. Named modules are only found via the modulepath.

The JRE is always on the modulepath, so its internal code cannot be accessed from code on the classpath.

Package Relationship to Modules

A package can only be accessed from one module. Hierarchical packages are treated as separate, so "java.util" and "java.util.logging" can exist in different modules. Only public fields and methods are accessible in the code of exported packages of other modules.

Classpath

All the code from classpath lives together in the "unnamed" module.

Organizatorium

  • Classpath and module path is mutually exclusive.