4

The schemagen goal of the jaxb2-maven-plugin fails on Java 9. The error is:

[Exception]: java.lang.ClassNotFoundException
[Message]: com.sun.istack.SAXParseException2
  • The jaxb2 plugin version is 2.3.1
  • The maven version is 3.5.0.
  • The maven compiler plugin version is 3.7.0.
  • All the plugins / dependencies are up-to-date.

I don't have any module-info.java file. The projects I build are legacy Java 8 projects, where the JDK has been upgraded from 1.8u144 to version 9.

Hopefully, the only plugin that fails at this moment is the jaxb2-maven-plugin. Everything else is OK, both at compile time and at execution time. The stack trace is:

org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
com.sun.tools.jxc.ap.SchemaGenerator.process(SchemaGenerator.java:103)
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:968)
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:884)
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.access$2200(JavacProcessingEnvironment.java:108)
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1206)
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1315)
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1246)
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:922)
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:100)
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:142)
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:96)
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:90)
com.sun.tools.jxc.SchemaGenerator$Runner.compile(SchemaGenerator.java:261)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:564)
com.sun.tools.jxc.SchemaGenerator.run(SchemaGenerator.java:166)
org.codehaus.mojo.jaxb2.schemageneration.AbstractXsdGeneratorMojo.performExecution(AbstractXsdGeneratorMojo.java:372)
org.codehaus.mojo.jaxb2.AbstractJaxbMojo.execute(AbstractJaxbMojo.java:257)
org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)
org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)
org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)
org.apache.maven.cli.MavenCli.execute(MavenCli.java:993)
org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345)
org.apache.maven.cli.MavenCli.main(MavenCli.java:191)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:564)
org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Stéphane Appercel
  • 1,427
  • 2
  • 13
  • 21
  • You might want to read [How to express dependency on java-ee features in Java9](https://stackoverflow.com/questions/46084751/how-to-express-dependency-in-maven-on-java-ee-features-for-transition-to-java-9) – Naman Sep 22 '17 at 03:49
  • Adding --add-modules javax.xml.bind early fails with: module not found: javax.xml.bind. Adding --add-modules java.se.ee without additional dependencies for the jaxb2-maven-plugin (as in the proposed solution) does not solve the problem either. – Stéphane Appercel Sep 22 '17 at 06:15
  • Could you share your `module-info.java` and the `pom.xml` for the module as well in that case? – Naman Sep 22 '17 at 06:16
  • I don't have a module-info.java file yet. I was performing a smoke build test of our legacy projects just by upgrading the JDK to version 9, in a first step. – Stéphane Appercel Sep 22 '17 at 06:22
  • 1
    Could you please update the question with this detail and also add the complete stacktrace to make the question and answers useful. – Naman Sep 22 '17 at 06:28
  • @StéphaneAppercel the correct module name is `java.xml.bind` , not `javax.xml.bind` – Tibor Blenessy Oct 02 '17 at 15:26
  • Thanks @TiborBlenessy. For the sake of clarity, it's still required to add the jaxb-jxc-jdk9 dependency to the jaxb2-maven-plugin to make it work, as in the proposed solution. – Stéphane Appercel Oct 02 '17 at 23:41

3 Answers3

3

I could fix the problem by adding the following dependencies to the plugin:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-jxc-jdk9</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>javax.activation-api</artifactId>
        <version>1.2.0</version>
    </dependency>
</dependencies>

For the xjc goal, I assume that it's needed to add the following dependencies to the plugin:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-xjc-jdk9</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>javax.activation-api</artifactId>
        <version>1.2.0</version>
    </dependency>
</dependencies>

Of course this is a workaround until a new version of the plugin is released.

Stéphane Appercel
  • 1,427
  • 2
  • 13
  • 21
  • 3
    This answer fixes the plugin failure on Java 9 (kudos!), but exposes another issue. The generated XML schema does not have prefix specified for the XML schema elements (e.g. schema, element, complexType, simpleType), which causes issues when trying to parse the generated it. This issue does not come up when using jaxb2-maven-plugin on Java 8. Did Java 9 change in relation to the XML schema marshalling? – Ioannis Sermetziadis Nov 21 '17 at 00:04
1

I finally resolved issue using xjc directly:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
        <execution>
            <id>execute xjc</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>xjc</executable>
                <longModulepath>false</longModulepath>
                <arguments>
                    <argument>-extension</argument>
                    <argument>-no-header</argument>
                    <argument>-d</argument>
                    <argument>src/main/java</argument>

                    <argument>-p</argument>
                    <argument>my.package.name</argument>
                    <argument>-b</argument>
                    <argument>src/main/resources/mybindings.xjb</argument>
                    <argument>src/main/resources/myschema.xsd</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>
7er
  • 91
  • 4
  • 1
    The OP was talking about the `schemagen` goal. Note that your approach won't work with Java 11 + because `xjc` exectuable is removed. – Dave Moten Jan 09 '19 at 23:48
  • It is enough to have jdk8/bin in PATH for maven to run xjc. – 7er Apr 16 '21 at 05:57
0

I was getting similar compilation errors while building maven with Java 17 and maven-jaxb2 plugin 2.3.0 . I tried latest version as well but got Mojo Exception while building the same with Maven . After researching a bit I was able to fix the issue using below implementation for jaxb2-plugin ,

Java - 17

Jaxb2-maven-plugin - 0.14.0

Spring Boot Maven - 2.7.4

In properties Tag add below version , <maven-jaxb2-plugin.version>0.14.0</maven-jaxb2-plugin.version>

In Build Tag ,

<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>${maven-jaxb2-plugin.version}</version>
<executions>
    <execution>
        <id>example-xmls</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <generateDirectory>${project.basedir}/src/main/java/com/cumulocity/exchnage_Demo</generateDirectory>
            <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
            <schemaIncludes>
                <include>**/*.xsd</include>
            </schemaIncludes>
            <bindingDirectory>${project.basedir}/src/main/resources/</bindingDirectory>
            <bindingIncludes>
                <include>**/*.xjb</include>
            </bindingIncludes>
        </configuration>
    </execution>
</executions>
<dependencies>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.3</version>
    </dependency>
</dependencies>
</plugin>   

After this JAXB-2 Implementation was successfully generated . Make sure to add below dependency as well helpful to provide implementation during runtime (When we run the main class)

<dependency>
         <groupId>org.glassfish.jaxb</groupId>
         <artifactId>jaxb-runtime</artifactId>
         <version>2.3.3</version><!--$NO-MVN-MAN-VER$-->
         <scope>runtime</scope>
    </dependency>

This should work for all those struggling with Java 17 and spring boot version 2.7.4+ with jabxb2 maven plugin.