mac java 9 gradle ClassNotFoundException: javax.xml.bind.JAXBElement when building

余生颓废 提交于 2020-02-21 13:42:46

问题


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.


回答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

...



回答2:


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"]
}



回答3:


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

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



来源:https://stackoverflow.com/questions/47429681/mac-java-9-gradle-classnotfoundexception-javax-xml-bind-jaxbelement-when-buildi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!