Cannot be cast to class - they are in unnamed module of loader 'app'
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the world of Java programming, the error message "Cannot be cast to class - they are in unnamed module of loader 'app'" is one that developers might encounter when dealing with class casting issues within modular or non-modular applications. Addressing this error involves understanding Java's module system, class loaders, and the casting mechanism. Let's delve into the technical explanations and examples to clarify the causes and solutions.
Understanding the Error
Java's module system, introduced in Java 9, allows developers to encapsulate packages and manage dependencies more systematically. However, when working with class casting, the error "Cannot be cast to class - they are in unnamed module of loader 'app'" can arise, mostly because of class loading issues where two classes seem identical but belong to different class loaders or modules.
Causes
- ClassLoader Mismatch: When two classes are loaded by different class loaders, they may represent different types even if they originate from the same class file. In Java, the
ClassLoaderis responsible for loading classes into memory. If classes are loaded by different class loaders, Java treats them as distinct entities. - Unnamed Module: In Java, a module is a collection of packages. An "unnamed module" doesn't belong to any named module and is typically used to provide backward compatibility with older Java code that doesn't adhere to the module system. The error often signifies that one or both classes are part of an unnamed module, complicating type recognition.
- ClassPath Issues: If the same class exists in multiple locations on the classpath, and different class loaders have picked different versions, this can lead to casting issues.
Example Scenario
Consider an example where the application uses a third-party library loaded into multiple class loaders:
Here, instance2 cannot be cast to SomeClass because instance1 and instance2 are instances of SomeClass loaded by different class loaders.
Solutions
- Ensure a Single ClassLoader: If possible, ensure that all classes are loaded by the same class loader. This could mean rearranging your application setup or combining dependencies into a single loader path.
- Refactor Modules: If you're using Java's module system, refactor your modules to include all necessary classes under the same module, reducing the chance of ambiguous class loading.
- Analyze ClassPath: Use tools such as
jpsandjstackto check and troubleshoot what classes are loaded and from where. This can help identify classpath issues that can lead to duplicate classes being loaded. - Debugging Class Loaders: Utilize logging and debugging features to introspect details about how classes are loaded. This insight can inform decisions on restructuring or optimizing the class loading process.
Example Correction
To correct the example error, loading the classes using the context class loader might help:
Summary Table
| Issue | Cause | Solution |
| ClassLoader Mismatch | Different loaders, same class | Use a unified ClassLoader |
| Unnamed Module | Classes aren't in named module | Refactor into named modules |
| ClassPath Issues | Duplicate classes in path | Analyze and optimize classpath |
Additional Details
ClassLoader Hierarchy
In Java, the class loader hierarchy follows the parent-delegation model. The application class loader typically delegations to the extension class loader, which in turn delegations to the bootstrap class loader. Understanding this hierarchy can help debug class loading issues akin to those seen in the "Cannot be cast to class" error.
Compatibility with Legacy Applications
When transitioning legacy applications to newer Java versions, using unnamed modules ensures backward compatibility while providing an interim solution before fully adopting the module system. While this helps maintain functionality, it demands diligence to prevent class loading issues.
Error messages related to class casting can be a confusing stumbling block but understanding Java modules and class loader intricacies enables developers to troubleshoot effectively. Implementing solutions that ensure consistent class loading provides a stable pathway for building robust Java applications.
Related reading
- Cannot convert from [java.lang.String] to [com.example.demo.User]
- Cannot create a reference to an object with a NULL id mongo hibernate-mongo and spring boot
- Cannot create namespace in multi-document transactionMongoDB 4.0, Spring Data 2.1.0, Spring Boot
- Cannot deserialize value of type java.util.Date from String
- Cannot change version of project facet Dynamic Web Module to 3.0?
- Cannot Connect from Kafkacat running in docker to Kafka broker running locally on windows machine
- Cannot disable Spring Cloud Config via spring.cloud.config.enabledfalse
- Cannot find MessageMatcherDelegatingAuthorizationManager class while trying to configure websocket security in Spring Boot 3

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.