javac --add-modules java.xml.bind for annotation processor?

Eirik Bjørsnøs eirbjo at gmail.com
Sun Nov 20 21:57:04 UTC 2016


Hi there!

I'm giving Java 9 and Jigsaw a spin on a moderately complex project.
(Interestingly, the project is itself a module system, so we do stuff like
annotation processing and dynamic reloading of class loader graphs and
services)

I'm experienced enough with advanced Java to have shot myself in the foot
with a java.lang.ClassNotFoundException: java.lang.ClassNotFoundException
:-) However, I've only recently been looking into Jigsaw in concrete terms.
So please be kind when I expose my ignorance :-)

Initially, I just want to see if I can get the project to compile on Java 9.

So I'm using --add-modules to javac here and there to add modules not
available to the unnamed module by default. (Not making any proper modules
yet, that will be the next step).

This seems to be working fine, except for one of our annotation processors,
which happens to use JAXBContext from java.xml.bind.

I added --add-modules to javac (via Maven configuration), but my annotation
processor still can't load JAXBContext and fails with this message:

$ java -version
java version "9-ea"
Java(TM) SE Runtime Environment (build 9-ea+144)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+144, mixed mode)

$ mvn clean install -e

This fails with:

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
at
com.example.processor.ProcessorUsingJavaXmlBind.process(ProcessorUsingJavaXmlBind.java:24)
at
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:959)
at
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:875)
at
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.access$2100(JavacProcessingEnvironment.java:106)
at
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1182)
at
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1290)
at
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1260)
at
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:939)
at
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl$1.call(JavacTaskImpl.java:106)
at
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl$1.call(JavacTaskImpl.java:100)
at
jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:137)
... 28 more

I've confirmed that this reproduces using only javac:

$ cd example-module

$ javac -d  target/classes -classpath
target/classes:$HOME/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar:
-sourcepath src/main/java/ -target 1.9 -source 1.9 --add-modules
java.xml.bind src/main/java/com/example/module/SomeClass.java

java.xml? true
java.xml.bind? false


An annotation processor threw an uncaught exception.
Consult the following stack trace for details.
java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
at
com.example.processor.ProcessorUsingJavaXmlBind.process(ProcessorUsingJavaXmlBind.java:24)
at
jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:959)


I've reduced this to a minimal-ish test case, available on Github:

https://github.com/eirbjo/java-xml-bind-processor

Here's the annotation processor:

https://github.com/eirbjo/java-xml-bind-processor/blob/master/annotation-processor/src/main/java/com/example/processor/ProcessorUsingJavaXmlBind.java

The class under compilation and the annotation:

https://github.com/eirbjo/java-xml-bind-processor/blob/master/example-module/src/main/java/com/example/module/SomeClass.java
https://github.com/eirbjo/java-xml-bind-processor/blob/master/example-module/src/main/java/com/example/module/SomeAnnotation.java


Any clues?

Cheers,
Eirik.


More information about the jigsaw-dev mailing list