Cannot be cast to class - they are in unnamed module of loader 'app'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding the "Cannot be cast to class" Error in Java Modules
In Java, the "Cannot be cast to class" error is one of the common reflective operation issues developers may face. This typically occurs when trying to dynamically cast an object to a class it is not an instance of. This becomes especially pertinent with the introduction of the Java Module System (Jigsaw) in Java 9, which provides a more robust encapsulation mechanism by dividing the JDK into modules.
Technical Explanation
When the error message includes the part "they are in unnamed module of loader 'app'," it provides a hint that the classes involved are loaded by the application's class loader and are not defined in a named module. In the Java Platform Module System, an "unnamed module" refers to the collection of all classes on the classpath, as opposed to a "named module" which is self-contained with explicit export and require statements.
Common Scenarios for this Error
- ClassLoader Issues: If different class loaders load the same class, they are considered distinct and not castable to each other even though their source code might be identical.
- Unintended Class Hierarchies: If a framework or library dynamically mingles different class hierarchies that don't align.
- Reflections: Incorrect use of Java Reflection may force a cast to a class type that the instance object does not support.
Example Scenario
Consider the following simple example:
In the above example, instance1 and instance2 might not belong to the same class loader. This would throw a ClassCastException due to differing class loader hierarchies even if instance1 and instance2 look identical.
Additional Details
Java Modules Overview
- Named vs. Unnamed Modules: Named modules explicitly declare their requirements and exposed packages, providing better encapsulation and reducing accessibility pitfalls.
- Modularization: Enhancements in modularization improve both application size and performance, as only required modules are loaded, reducing memory footprint and potential for conflicts.
Best Practices
- Use Consistent Class Loaders: Ensure that classes loaded in reflective operations come from a single, unified class loader environment.
- Refactor Codebase: Assess and refactor the code to adopt a module-path instead of continuing with a traditional class-path approach to leverage the encapsulation benefits of Java’s module system.
- Avoid Reflection For Casting: Employ compile-time type checks wherever possible to prevent such runtime exceptions.
- Diagnostic Tools: Utilize Java Module Analysis tools or invoke
jdepsto better understand dependencies and relationships between different modules.
Summary Table
| Issue/Concept | Description | Best Practices |
| ClassLoader Discrepancies | Classes loaded by different class loaders are incompatible | Use consistent class loaders across app |
| Dynamic Class Hierarchies | Systematically deriving class hierarchies through reflection or dynamic loading | Refactor to standardize class hierarchy |
| Using Java Modularity | Employs explicit modules within application to organize and manage code | Shift from class-path to module-path |
| Reflection Induced Cast Error | Excessive or incorrect use of reflection causing typecast exceptions | Limit reflection usage; prefer explicit type checks |
| Named vs. Unnamed Modules | Named modules are explicitly declared, unnamed modules are those existing on the classpath | Migrate to using named modules for clarity |
Conclusion
The error "Cannot be cast to class... they are in unnamed module of loader 'app'" often flags deeper issues in how Java applications are organized, particularly with traditional and legacy classpath setups. Leveraging Java's module system can significantly reduce confusion around class loading and typecasting, enhancing both the reliability and performance of Java applications.

