Cannot be cast to class - they are in unnamed module of loader 'app'
Asked Answered
S

8

153

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

Sixfold answered 5/2, 2019 at 14:43 Comment(5)
not very sure about wsdl here...but, did you try adding a module-info.java to your project and/or ensure that the module you're depending upon (for classes org.apache.cxf.endpoint.ClientImpl) is resolved on the modulepath rather than the classpath.Zincography
There is no “because” in the error message. All this addendum tells you, is, that both classes are located in the same module, the unnamed module of loader 'app', which helps the reader to understand that this problem is entirely unrelated to modules. ClientImpl simply is not a subtype of XigniteCurrenciesSoap; it's an ordinary ClassCastException.Tejeda
In my case it was a little bit different. Class Cast Exception sometime happen because of the conflict between different version of the java. in pom.xml i set to use "<java.version>1.8</java.version> but in Intellje IDE i set to use java version 11. after changing java version from 11 to 8 problem solved for me.Cormorant
I was using Kotlin alongside Java , simply recompiling the kotlin class resolved the issueDelila
It is getting when using java version 17. Same code working fine with java 1.8Aerograph
S
92

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.

Solana answered 2/9, 2019 at 8:23 Comment(6)
I had a similar problem and thought it was impossible to be a ClassCastException. After finding this answer I properly examed the exception again and it turned out I was extending from a superclass with a same name in a different package. So even though it might look like a different problem, it really is a ClassCastException.Dogoodism
At the time I asked the question, I was still fairly new with Apache CXF. The reason for the class cast exception was because I was using JaxWsClientFactoryBean instead of JaxWsProxyFactoryBean. The former returns a ClientImpl while the latter returns a proxy that you must cast. I'm working on something new for work and remembered this question.Sixfold
In my case, I am trying to convert List<Object> to List<MyInterface> and am getting same problemEvenson
In my case, there are 2 similar classes in the test & app modules of my project, and it was trying to cast MyClass from the app module to the MyClass from the test one. Since the app had been updated recently, I got to transfer the MyClass changes into the test module, and now it works! @Tejeda & @orirab, thanks for your hints! :)Derna
I faced this error during a migration of an old application written in Spring 3 running fine in Java 8 and JBoss EAP 7.4, to Spring 5 / Java 11 / Wildfly 26.1.3. The old application was having some java classes generated from wsdl with xjc on jdk8 for a SOAP webservice that caused the above problem. In order to solve I have needed to regenerate the classes with jdk11 by using maven jaxb2-maven-plugin 2.5.0 version. That plugin version was needed due to generate classes with import javax.xml.bind.* and maintain the application dependency with jakarta.xml.bind-api 2.3.3Eldoria
could you please take a look at my case, because I do not really understan it #78394944Commandeer
B
3

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.

Burnish answered 26/4, 2020 at 18:29 Comment(0)
P
2

I was getting very similar above exception.

java.lang.ClassCastException: class [B cannot be cast to class org.apache.avro.generic.GenericRecord ([B is in module java.base of loader 'bootstrap'; org.apache.avro.generic.GenericRecord is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader 

Root cause was my Gradle dependency had exclude statement, i.e.

exclude group: 'org.springframework.cloud', module: 'spring-cloud-stram-binder-kafka',

I commented it as below and things were working fine after that:

implementation ('com.xyz.lux:lux-acitor:1.25.0') {
  //exclude group: 'org.springframework.cloud', module: 'spring-cloud-stream-binder-kafka'
  exclude group: 'org.slf4j', module: 'slf4j-reload4j'
  exclude group: 'io.confluent', module: 'confluent-log4j'
}
Portly answered 14/9, 2022 at 21:5 Comment(0)
S
1

I had the same problem. The problem in my case was that I already had a class with the same name in another place. So try changing the class name.

Stellarator answered 6/5, 2022 at 9:9 Comment(0)
S
1

I had the same issue using spring cloud stream kafka, when i tried to consume the message it throw the exception.

In my case, it is because Spring boot needed any kind of dependency which would allow the Serde (serialisation or deserialisation). Just added the json one.

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
            <version>3.0.2</version>
            <scope>compile</scope>
        </dependency>

after that i was able to keep moving forward. Remember if you have Spring Web dependency then this above dependency is automatically included

Schaaff answered 8/2, 2023 at 12:47 Comment(0)
S
1

In my case I had a parent class with the following lombok annotations

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Parent {
...
}

and then the child class looked like this

public class Child extends Parent {}

In the end I got it working by adding these two lombok annotations to the child class

@SuperBuilder
@EqualsAndHashCode(callSuper = true)
Sisterly answered 16/10, 2023 at 12:48 Comment(1)
have the same issue and did not work :(. You can take a look to my case here: #78394944Commandeer
I
1

In my case: i used one of auto-generators to generate data-classes from json-schema. After that i renamed Top-level data class i got from auto-generator. Inside of that class was field with same name. Jackson couldn't deserialize it.

After i renamed top-level data class issue was fixed

Improvise answered 24/11, 2023 at 12:32 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Amenable
A
1

In my case, the error popped up because two of my classes were implementing Serializable and I accidentally kept their serialVersionUID parameter the same.

private static final long serialVersionUID = -8885817712041252438L;

Make sure you keep this parameter unique for all classes.

Explanation:

The docs for java.io.Serializable explain pretty well.

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long

Ammieammine answered 18/12, 2023 at 0:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.