Automatic Module Naming from JAR Filename
What you'll learn: How the Java module system automatically generates valid module names from JAR filenames when no explicit module descriptor exists.
When you place a non-modular JAR on the module path, it becomes an automatic module. But how does JPMS know what to call it? The system derives a module name directly from the JAR filename using a predictable transformation process.
The Naming Algorithm
The module system follows these steps to convert a JAR filename into a valid module name:
- Remove the
.jarextension –commons-lang3-3.12.0.jarbecomescommons-lang3-3.12.0 - Strip version suffixes – Numbers and trailing separators at the end are removed:
commons-lang3-3.12.0becomescommons-lang3 - Replace invalid characters with dots – Any character that isn't alphanumeric or a dot is converted to a dot: hyphens become dots, so
commons-lang3becomescommons.lang3 - Remove duplicate dots – Multiple consecutive dots collapse to one
- Remove leading/trailing dots – Clean up the edges
Example Transformations
guava-31.1-jre.jar→guavajackson-databind-2.13.0.jar→jackson.databindmy_library-1.0-SNAPSHOT.jar→my.library
Why This Matters
These automatic names are what you'll use in requires directives when depending on automatic modules. If the filename is messy or changes between versions, the derived module name changes too—which can break your module declarations.
Key Takeaway: Automatic module names are derived from JAR filenames by removing version numbers, converting non-alphanumeric characters to dots, and cleaning up the result—making filename consistency crucial for stable module dependencies.