How do you organize tests in a modular Java project?

Welcome to Testing In The Modular World! Which kind of tests do you want write? Extra-module tests: Create a test-only project (no “src/main” directory) and declare a “src/test/java/module-info.java” module descriptor. In-module tests: As it was from Day 1 you need to “blend in”/merge/shadow your test classes into your main classes or vice versa. Here you … Read more

Why did Java 9 introduce the JMOD file format?

The purpose of JMODs are not well documented and existing documentation is rather sparse. Here is an in-depth explanation of system, from my understanding. Parts of this answer are rather long, verbose, partially redundant, and a tough read. Constructive, structural, or grammatical edits are more than welcome to improve readability for future readers. Short(er) Answer … Read more

Accessing resource files from external modules

// to scan the module path ClassLoader.getSystemResources(resourceName) // if you know a class where the resource is Class.forName(className).getResourceAsStream(resourceName) // if you know the module containing the resource ModuleLayer.boot().findModule(moduleName).getResourceAsStream(resourceName) See a working example below. Given: . ├── FrameworkCore │ └── src │ └── FrameworkCore │ ├── com │ │ └── framework │ │ └── Main.java │ … Read more

Scanning classpath/modulepath in runtime in Java 9

The following code achieves module path scanning in Java 9+ (Jigsaw / JPMS). It finds all classes on the callstack, then for each class reference, calls classRef.getModule().getLayer().getConfiguration().modules(), which returns a a List<ResolvedModule>, rather than just a List<Module>. (ResolvedModule gives you access to the module resources, whereas Module does not.) Given a ResolvedModule reference for each … Read more