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

Eirik Bjørsnøs eirbjo at gmail.com
Sun Nov 20 22:43:44 UTC 2016


Claes,

I tested this using javac directly on the command line, like this

$ cd example-module
$ javac -J--add-modules -Jjava.xml.bind -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
 src/main/java/com/example/module/SomeClass.java

java.xml? true
java.xml.bind? true
class javax.xml.bind.JAXBContext

The above works (you can see that the processor is applied prints some
debug info)

Maven seems to the -J arguments somehow. Here's my current
maven-compiler-plugin configuration:

<configuration>
    <fork>true</fork>
    <compilerArgs>
        <arg>-J--add-modules</arg>
        <arg>-Jjava.xml.bind</arg>
    </compilerArgs>
</configuration>

This gives the following output from mvn clean compile -X :

[DEBUG] Command line options:
[DEBUG] -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/generated-sources/annotations
src/main/java/com/example/module/SomeClass.java
src/main/java/com/example/module/SomeAnnotation.java -s
target/generated-sources/annotations -g -nowarn -target 1.9 -source 1.9

(Absolute paths cleaned up for readability..)

Eirik.



On Sun, Nov 20, 2016 at 11:23 PM, Claes Redestad <claes.redestad at oracle.com>
wrote:

> Hi Eirik,
>
> compiling com.example.processor.ProcessorUsingJavaXmlBind standalone
> seems to work fine with --add-modules java.xml.bind - instead it
> appears as if maven-compiler-plugin doesn't preserve argument order, so
> you need to use the joined argument form in your pom.xml:
>
> <arg>--add-modules=java.xml.bind</arg>
>
> I tried this on your minified example, and seems to work. Thanks!
>
> /Claes
>
>
> On 2016-11-20 22:57, Eirik Bjørsnøs wrote:
>
>> 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(Proc
>> essorUsingJavaXmlBind.java:24)
>> at
>> jdk.compiler/com.sun.tools.javac.processing.JavacProcessingE
>> nvironment.callProcessor(JavacProcessingEnvironment.java:959)
>> at
>> jdk.compiler/com.sun.tools.javac.processing.JavacProcessingE
>> nvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:875)
>> at
>> jdk.compiler/com.sun.tools.javac.processing.JavacProcessingE
>> nvironment.access$2100(JavacProcessingEnvironment.java:106)
>> at
>> jdk.compiler/com.sun.tools.javac.processing.JavacProcessingE
>> nvironment$Round.run(JavacProcessingEnvironment.java:1182)
>> at
>> jdk.compiler/com.sun.tools.javac.processing.JavacProcessingE
>> nvironment.doProcessing(JavacProcessingEnvironment.java:1290)
>> at
>> jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAn
>> notations(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.handleExc
>> eptions(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(Proc
>> essorUsingJavaXmlBind.java:24)
>> at
>> jdk.compiler/com.sun.tools.javac.processing.JavacProcessingE
>> nvironment.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/maste
>> r/annotation-processor/src/main/java/com/example/processo
>> r/ProcessorUsingJavaXmlBind.java
>>
>> The class under compilation and the annotation:
>>
>> https://github.com/eirbjo/java-xml-bind-processor/blob/maste
>> r/example-module/src/main/java/com/example/module/SomeClass.java
>> https://github.com/eirbjo/java-xml-bind-processor/blob/maste
>> r/example-module/src/main/java/com/example/module/SomeAnnotation.java
>>
>>
>> Any clues?
>>
>> Cheers,
>> Eirik.
>>
>>


More information about the jigsaw-dev mailing list