5

I'm trying to build a gradle project on Mac with Java 9, but I'm getting this error:

FAILURE: Build failed with an exception.

Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBElement

Java version is:

java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

How do I add JAXB to the classpath? This projects builds on Windows and other computers OK without adding JAXB.

Thomas Kessler
  • 1,717
  • 3
  • 16
  • 27

3 Answers3

1

You're seeing this because of changes in Java 9. Here's how to solve that: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9

However, if your project is written for Java 8 (or older), what I would really suggest is letting Gradle know about that in your build.gradle:

...

plugins {
    id 'java'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

...
Gorazd Rebolj
  • 803
  • 6
  • 10
  • I tried adding sourceCompatibility = 1.9 and targetCompatibility = 1.9 to my build.gradle, but it didn't help. I don't know where to put: --add-modules java.xml.bind in my build.gradle file. Can you be a little more specific on how to tell gradle to include the modules? – Thomas Kessler Nov 22 '17 at 08:35
  • You have to set compatibility to 1.8 and NOT 1.9 – Gorazd Rebolj Nov 22 '17 at 08:39
  • This answers your question how to add the java arguments in gradle: https://stackoverflow.com/questions/40530898/gradle-does-not-honor-add-modules-jvm-argument-in-jdk9#40533958 – Gorazd Rebolj Nov 22 '17 at 08:40
  • `--add-modules java.xml.bind` is a short term solution. The next phase, to remove the java.xml.bind module completely is already in draft form, see: http://openjdk.java.net/jeps/8189188 – Alan Bateman Nov 22 '17 at 09:23
0

It works when adding the following to build.gradle:

compile 'javax.annotation:jsr250-api:1.0'

tasks.withType(AbstractCompile) {
    options.compilerArgs += ["--add-modules", "java.xml.bind"]
}

tasks.withType(Test) {
    jvmArgs += ["--add-modules", "java.xml.bind"]
}
Pratik Patil
  • 3,662
  • 3
  • 31
  • 31
-1

You can add the following under dependencies block to solve missing JAXBElement:

runtime('javax.xml.bind:jaxb-api')

Dmitry
  • 1,427
  • 15
  • 16