javajava-8jaxbjava-11

JAXB-dependent app compiled for Java 8 - possible to run as-is on JDK 11?


I'm using JAXB 2.3 in a desktop app to generate Java code from a .xsd file.

My app is currently compiled to Java 8, but I need to run it on OpenJDK 11.

My understanding is that compiling it to Java 11 would require using Jakarta EE instead of Java EE.

To avoid upgrading everything, I'm trying to just run the app still compiled for Java 8 and rely on the backwards compatibility of the JDK, but I'm getting a NoSuchMethodError as a result of bad XML parsing.

Is it possible to use JDK backwards compatibility here or do I have to migrate to Java 11?


Solution

  • tl;dr

    1. Add the JAXB interfaces (API) to your project.
    2. Add an implementation of JAXB to your project.

    (Or at runtime have the interfaces and implementation available on the Classpath.)

    JAXB removed from Java SE

    JAXB is among the Java EE features deprecated from Java SE 9, and removed in Java 11. JAXB is now known as Jakarta XML Binding.

    As explained in that linked JEP 320, you merely need to add an implementation of JAXB to your project as a library.

    If you wish to code to the JAXB interfaces rather than to a specific implementation product, then you’ll also need to include the JAXB interfaces as a library in your project.

    I am assuming you are building a console app or desktop GUI app, not a Web app.

    You said:

    My understanding is that compiling it to Java 11 would require using Jakarta EE instead of Java EE.

    No, not quite accurate. You just need two libraries added to your project:

    You could use the last Java EE version of the APIs. Those should work under Java 11, though you should test first.

    But be aware: The last version of Java EE was republished as the first version of Jakarta EE. The initial Jakarta EE is virtually identical to the last Java EE.

    That initial release of Jakarta EE continued to use the javax.* package naming used in Java EE. Successive versions of Jakarta EE changed to using the jakarta.* packaging, but not in the initial release.

    So I recommend the initial version of Jakarta EE (as shown below). I would guess you might see minor updates on the Jakarta EE side but not the Java EE because the Jakarta project is actively developed and maintained while the Java EE project has been phased out.

    Jakarta XML Binding

    JAXB was renamed more clearly to Jakarta XML Binding. The version 2.3 release continued using the javax.* package naming.

    The last update to that release was version 2.3.3 available at a Maven repository such as this one.

    <!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>2.3.3</version>
    </dependency>
    

    Eclipse Implementation of JAXB

    One implementation of that API is Eclipse Implementation of JAXB, originally from Sun Microsystems, sold to Oracle, then donated to the Eclipse Foundation.

    I am guessing that you would want version 2.3.9 of that for use with Jakarta XML Binding 2.3.x. But that is based on a glance at the docs; I’ve not tried it.

    I would expect you could find other implementations as well.

    <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.9</version>
    </dependency>