David Sackstein
David Sackstein

Reputation: 560

How can I import JAXB into a Java 11 IntelliJ (Gradle) project?

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?

Upvotes: 7

Views: 22786

Answers (2)

Raging bull
Raging bull

Reputation: 1

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
}

Upvotes: 0

pirho
pirho

Reputation: 12235

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.

Upvotes: 10

Related Questions