Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 292
David Sackstein Avatar asked Jan 05 '19 19:01

David Sackstein


People also ask

How do I use JAXB in Gradle?

Tutorial: Using JAXB with Gradle Create a new Gradle project called com.vogella.xml.jaxb.gradle. Adjust your build.gradle file to include the javax.xml.bind and org.eclipse.persistence libraries as dependencies to your Gradle plug-in.

How to import Gradle project to IntelliJ IDEA?

Step 1 – Open IntelliJ IDEA and Welcome Screen appears. Click the Open button present on Welcome Screen. Step 2 – Navigate to your Gradle project and select the top-level folder. Select the project you want to Import. Select the OK button to proceed to the next screen. Step 3 – A screen appears to Open or Import project.

How do I add JAXB to a project in Java 11?

In Java 9 and Java 10 we need to use the --add-modules=java.xml.bind option. In Java 11, JAXB has been removed from JDK and we need to add it to the project as a separate library via Maven or Gradle. In our examples, we use JDK 11 and Maven to create our applications.

How do I enable JAXB in IntelliJ IDEA?

Configure IntelliJ IDEA to use JAXB and XMLBeans Download and install the XMLBeans tool. In the Settings/Preferences dialog Ctrl+Alt+S, open the Plugins page and make sure that the bundled Java EE: Web Services (JAX-WS) plugin is enabled.


1 Answers

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.

like image 181
pirho Avatar answered Dec 01 '22 19:12

pirho