Home > Back-end >  java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory

Time:05-27

I am new to Java and Gradle, I am trying to build a simple REST API, here are my dependencies

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.7.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

    implementation("org.springframework.boot:spring-boot-starter-actuator:2.6.7")
    implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
    implementation 'com.sun.xml.bind:jaxb-core:4.0.0-M4'
    implementation 'com.sun.xml.bind:jaxb-impl:4.0.0-M4'
}

I am receiving the following error: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory

I have seen similar questions but could not find a solution

CodePudding user response:

Your JAXB API and implementation versions don't match. jaxb-core 4.0.0-M4 depends on jakarta.xml.bind:jakarta.xml.bind-api 4.0.0. That's completely incompatible with your JAXB API version. The biggest difference is that your JAXB API version uses the javax package prefix and the Jakarta API version uses the jakarta package prefix. I'm guessing that Spring wants to use an implementation compatible with the API version you're using (Spring hasn't switched to Jakarta EE 9 yet).

I was checking to see if there was a Spring Boot starter for JAXB or something like that, but I couldn't find any. That means you'll probably need to provide your own implementation. I suggest that you switch to the Jakarta EE 8 version instead: https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api/2.3.3 for the API, and https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime/2.3.6 for the implementation. Both are the latest versions that still use the javax package prefix.

  • Related