7

I have been using JAXB in a Java project using JDK 8. Having migrated to JDK 11, the JAXB package name is no longer recognized. I have also not found a way to add JAXB as a dependency in gradle. How can I import JAXB into my project?

David Sackstein
  • 500
  • 1
  • 5
  • 19

2 Answers2

10

You need to include JAXB API and choose from one of the JAXB implementations because JAXB is is no more included by default in the JDK 11. You need to add some dependencies to your build.gradle.

So first:

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'

and if you decide to use for example MOXy, then something like:

compile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy', 
    version: '2.7.3'

See also this great explanation

This example using MOXy also requires jaxb.properties file containing information about JAXBContextFactory (see here chapter 2.1):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Advantage of this seems to be:

Because you do not need to change any application code, you can easily switch between different JAXB implementations.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • @MerajHussain You were correct. It would not work just like in my original answer. I have updated my answer to contain more information. – pirho Dec 17 '19 at 18:24
  • how to add the jaxb property in the build.gradle? – Meraj Hussain Dec 18 '19 at 04:54
  • @MerajHussain You do not add it to a gradle. It is a file that needs to be located at the same package as your model packages un-/marshalled. If you wish to use Sun's implementation you could see the required dependencies (Maven based but guess it is the same thing) [here](https://stackoverflow.com/q/53978612/6413377) – pirho Dec 18 '19 at 12:00
  • @MerajHussain above _your model packages_ means more specific package where your model classes are so each pkg seems to need its own – pirho Dec 18 '19 at 12:17
0

I had a build issue in my android flutter project and I spent quite a few hours hunting on this issue. I understood that Java 11 does not support certain JAR files out of the box and hence we had to include some dependency. But it was not straightforward. I nailed this down to a certain version issue in JAXB library. Finally, here is my working solution for android/build.gradle file. I added a few repositories (like google, mavenCentral) and also the version of jaxb-api of 2.3.0 is the one that worked. I remember 2.3.1 did not work at that time... but when I tried now it works. Its strange. Evidently, I am not an expert in this area. I was just thankful that It worked out finally... I am presenting the android/build.gradle for whatever salt its worth. Hope this helps someone.

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://repo1.maven.org/maven2/")
        }
        jcenter()
    }

    dependencies {
        classpath "javax.xml.bind:jaxb-api:2.3.0"
        classpath 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2'
        classpath 'org.glassfish.jaxb:jaxb-runtime:2.3.2'
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://repo1.maven.org/maven2/")
        }
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}