Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot be cast to class because they are in unnamed module of loader 'app'

I'm trying to create a bean from sources that were generated by wsdl2java.

Every time I try to run my Spring Boot app, I get the following error:

Caused by: java.lang.ClassCastException: class org.apache.cxf.endpoint.ClientImpl cannot be cast to class com.xignite.services.XigniteCurrenciesSoap (org.apache.cxf.endpoint.ClientImpl and com.xignite.services.XigniteCurrenciesSoap are in unnamed module of loader 'app')

I'm not sure how exactly I'm to include generated sources in my main Spring Boot application as a module.

My directory structure is:

├── build │   └── generatedsources │       └── src │           └── main │               └── java │                   └── com │                       └── xignite │                           └── services │       └── src     └── main         ├── java         │   └── io         │       └── mateo         │           └── stackoverflow         │               └── soapconsumption         └── resources            └── wsdls 

Relevant system info:

openjdk version "11.0.1" 2018-10-16 OpenJDK Runtime Environment 18.9 (build 11.0.1+13) OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode) 
  • Spring Boot 2.1.2.RELEASE
  • Gradle 5.2

I've also uploaded the project onto Github here: https://github.com/ciscoo/soap-consumption-spring-boot

like image 338
Francisco Mateo Avatar asked Feb 05 '19 14:02

Francisco Mateo


People also ask

How do you handle ClassCastException in Java?

How to handle ClassCastException. To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What is ClassCastException in Java with example?

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. So, for example, when one tries to cast an Integer to a String , String is not an subclass of Integer , so a ClassCastException will be thrown.

Is ClassCastException checked or unchecked?

ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.

What is ClassCastException in Java?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.


2 Answers

I had a similar case, and (as mentioned by @Holger in the comment) the module info in the message is simply misleading - this is an actual case of trying to cast something to something that doesn't match it.

In your case, ClientImpl simply is not a subtype of XigniteCurrenciesSoap.

like image 175
orirab Avatar answered Sep 22 '22 02:09

orirab


The stacktrace is trying to tell you that you have casted XigniteCurrenciesSoap to ClientImpl.

Such as the example below:

Object returnObj= getXigniteCurrenciesSoap(); return (ClientImpl) returnObj; 

You have to find out where you did that in your code and fix it.

like image 43
Hotpot with Kraut Avatar answered Sep 23 '22 02:09

Hotpot with Kraut