Netbeans with JAXB Random ClassCastException ..cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
Asked Answered
M

8

22

I have downloaded the Soap messages from a SOAP Service and trying to mock the Soap service by returning the downloaded messages. the following code shows how I am Unmarshalling the Soap message into the required Response

    public static DataClientType unmarshallFile(String fileName) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(ClientSampleSoapResponseData.class.getResourceAsStream(fileName));
    xsr.nextTag(); // Advance to Envelope tag
    xsr.nextTag(); // Advance to Header
    xsr.nextTag(); // Advance to Body tag
    xsr.nextTag(); // Advance to getClientByAccountResponse
    xsr.nextTag(); // Advance to content of getClientByAccountResponse

    JAXBContext jc = JAXBContext.newInstance(GetClientByAccountResponse.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<GetClientByAccountResponse> je = unmarshaller.unmarshal(xsr, GetClientByAccountResponse.class);

    return je.getValue().getClientDataContract();
}

However I keep getting this ClassCastExeption which happens randomly. After a number of test iterations, it begins to happen. Sometimes a clean and build fixes it but sometimes it doesn't work.

java.lang.ClassCastException: com.x.X.X.X.GetClientByAccountResponse$JaxbAccessorF_clientDataContract cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:188)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:180)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:256)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)

I have tried other online suggestions such as reverting to old jaxb versions and using endorsed folders in the maven compiler configuration but it still happens

Any ideas on what could be causing it and possible solutions?

Thank u

Micronesia answered 22/3, 2013 at 11:37 Comment(1)
@TheDownVoter if you are going round voting peoples questions down, at least provide a reason or suggest something! Remember we are not all as smart as u think you are.Micronesia
M
38

Solved with the following code

@BeforeClass
public static  void init(){ 
    System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");
}

@AfterClass
public static void revert(){ 
    System.getProperties().remove("com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize");
}    

Parameter can also be set at JVM using

-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true
Micronesia answered 23/3, 2013 at 3:47 Comment(3)
Wow! You helped me a lot with this solution! Did you inspect why this optimization was causing so much trouble?Dewar
No. Moved on after problem was solved but I suspect its something to do with jdk versions.Micronesia
ruslan5t, Yes, it is a workaround that only hides the real problem.Underlet
S
5

I encountered the same error when I tried to upgrade JAXB to a newer version than what came with the JDK. Java encountered two or more instances of JAXB at runtime and could not decide which version to use.

In my case, the problem was that my application used web services, and I hadn't externalized JAX-WS as well. The application started out using com.sun.xml.bind.v2.runtime classes, but when it began working with a WSDL file, the internal JAX-WS tried to invoke com.sun.xml.internal.bind.v2.runtime classes. The error went away when I downloaded and installed JAX-WS, and I was able to continue the upgrade.

Sams answered 25/11, 2014 at 4:19 Comment(3)
can you share more details on how you installed JAX-WS? is it as sample as putting the jax-ws lib on the classpath?Folks
@Folks Pretty much. I believe that JAX-WS comes with something like 16 jars, with 2-4 related to JAX-B. I added all of the jars to my runtime classpath, and the error was resolved. Admittedly, it has been four years since this happened, and I can't remember the exact details. I pretty much just remember that my original issue was that External JAX-B called Internal JAX-WS, which then called Internal JAX-B. It was a coupling issue.Sams
If you're using Maven, then for me adding jaxws-rt (runtime-jar) helped. Note I added it for my test scope only. For real runtime, you are not going to do this, since JEE supposed to has it already. <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.3</version> <scope>test</scope> </dependency>Underlet
C
1

I ran into the same issue in one of my applications. In my case, the project was using a library compiled with Java 1.5 compatibility while the main project had compatibility with version 1.6. When I changed both to use 1.6 the problem went away. I hope this will be able to help someone since the problem can be quite frustrating and hard to track.

Convulsive answered 15/8, 2013 at 12:32 Comment(0)
B
1

The accepted solution worked for me when in Intellij, but I got the same error when running maven from the command line. Adding this to the configuration for maven-surefire-plugin solved the issue there as well:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <systemPropertyVariables>
                    <com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize>true</com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize>
                </systemPropertyVariables>
            </configuration>
        </plugin>
Bendigo answered 22/12, 2016 at 14:41 Comment(2)
This works for me when running maven from command line but not in Jenkins. Any idea what the problem could be?Ecbatana
Sorry, I don't know.Bendigo
P
1

I removed the dependency on separate jaxb-impl jar from the build.sbt. That works now.

Popular answered 19/7, 2017 at 8:34 Comment(0)
T
0

I had a similar issue, but I didn't / don't have a dependeny(-version-mismatch)-problem.

In my case, a class was missing a @XmlAccessorType, adding this annotation solved my problem. Adapted to your case, the solution would be:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD) // check, if @XmlAccessorType is missing
public class GetClientByAccountResponse {
  ..
Tuneless answered 10/5, 2018 at 12:39 Comment(0)
I
0

In my case, the problem was that my application used web services and spring data. I exclude artifact org.glassfish.jaxb that has that contains com.sun.xml.bind.v2.runtime.reflect.Accessor class and other in my pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
    </exclusion>
  </exclusions>
</dependency>

It's work for me!

Intoxication answered 10/3, 2021 at 9:20 Comment(0)
R
-3

Including jaxbiimpl jar during run time through your dependency manager in parent pom will resolve this issue. This solution is specific to maven projects.

Response answered 9/1, 2015 at 18:48 Comment(1)
Could anyone share why this answer was down voted? Not that I know, understand or believe it to be correct, only that I do not see on its face why it is incorrect.Christiechristin

© 2022 - 2024 — McMap. All rights reserved.