4

I'm building java classes based on an xsd using "jaxb-ri" in command line (xjc.bat). I want to set a namespace in an XmlType annotation using jaxb2-annotate_plugin but I don't know how to add this plugin to xjc classpath and acitvate it.

here is my external bindings.xjb file :

<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:annox="http://annox.dev.java.net"
              jxb:extensionBindingPrefixes="xjc annox"
              version="2.1">
    <jxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
        <jxb:bindings node="xs:complexType[@name='MyType]">
            <annox:annotateClass>@javax.xml.bind.annotation.XmlType(namespace="urn:myschematwo")</annox:annotateClass>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

My command line is :

D:/jaxb-ri/bin/xjc.bat -p "com.jaxb.mypackage" -d
"D:/myproject/src/main/java" D:/myschema.xsd -b "D:/bindings.xjb"
-extension

When I execute this command, I have an error message saying that "http://annox.dev.java.net" is not supported.

Thanks in advance.

lexicore
  • 42,748
  • 17
  • 132
  • 221
Samy
  • 121
  • 3
  • 9
  • Make sure that correct jars are within classpath e.g. `xjc.bat -classpath "jaxb2-basics-annotate-0.6.0.jar"` – Xstian Nov 04 '15 at 16:17
  • Thanks for your response, I did that but I got the following error ==> Exception in thread "main" java.lang.NoClassDefFoundError: org/jvnet/jaxb2_commons/plugin/AbstractParameterizablePlugin at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) – Samy Nov 04 '15 at 16:33
  • I think that missing also `jaxb2-basics-tools-0.6.0.jar`. Try to add it in the classpath. – Xstian Nov 05 '15 at 08:07
  • I looked for all missed libraries each time I had a NoClassFound but in final, I had the first error message saying that "http://annox.dev.java.net" is not supported. Here is the added libraries -classpath "../lib/jaxb2-basics-annotate-1.0.2.jar";"../lib/tools-0.4.1.5.jar";"../lib/commons-logging-1.1.1.jar";"../lib/jaxb2-basics-runtime-0.9.5";"../lib/annox-1.0.1.jar";"../lib/javaparser-1.0.11.jar";"../lib/jaxb2-basics-tools-0.9.5.jar" – Samy Nov 05 '15 at 08:45
  • You must remove the `"` character between libraries .. Should be `-classpath "../lib/jaxb2-basics-annotate-1.0.2.jar;../lib/tools-0.4.1.5.jar;../lib/comm‌​ons-logging-1.1.1.jar;../lib/jaxb2-basics-runtime-0.9.5;../lib/annox-1.0.1.ja‌​r;../lib/javaparser-1.0.11.jar;../lib/jaxb2-basics-tools-0.9.5.jar"` – Xstian Nov 05 '15 at 08:49
  • Still have the same error Binding spacename ---> "http://annox.dev.java.net" not supported. Do you maybe mean "http://java.sun.com/xml/ns/jaxb/xjc" ? – Samy Nov 05 '15 at 09:02
  • Yes .. we're missing an extraargument `-Xannotate` add it at the end of command. – Xstian Nov 05 '15 at 09:09
  • Unfortunately, Still get the same error – Samy Nov 05 '15 at 11:14
  • As last option , i've some doubts about the libraries versions. [See Here](http://static.highsource.org/jaxb2-basics/jaxb2-basics-annotate/dependency-management.html) – Xstian Nov 05 '15 at 14:07

1 Answers1

7

Disclaimer: I'm the author of jaxb2-annotate-plugin.

First, I don't provide sample for command line as (as far as I know) nobody uses command line or batch scripts for schema compilation. At least up to date there were no requests for that. There are samples for Ant and Maven, the one for Ant is the closes to the command line usage.

Next, I wonder what is in xjc.bat.

If it calls xjc.exe from Java distibution, this most probably won't work with third-party plugins at all, not just with the jaxb2-annotate-plugin. The reason is that the XJC version which is distributed with Java has a renamed package: com.sun.tools.** -> com.sun.tools.internal.**. This breaks, naturally compagtibility with plugins. So please make sure xjc.bat does not call the xjc.exe contained in the Java distro.

I hope xjc.bat calls com.sun.tools.xjc.XJCFacade from JAXB RI Jars.

To execute the plugin you'll need two things:

  • Include all the required dependencies to the classpath.
  • Turn the plugin on using -Xannotate command line parameter.

These dependencies are:

                <!-- JAXB2 Basics library -->
                <include name="jaxb2-basics-*.jar"/>
                <include name="jaxb2-basics-annotate*.jar"/>
                <!-- JAXB2 Basics library dependencies -->
                <include name="annox-*.jar"/>
                <include name="javaparser-*.jar"/>
                <include name="commons-beanutils-*.jar"/>
                <include name="commons-logging-*.jar"/>
                <include name="commons-lang3-*.jar"/>

Here's the full dependency tree (with versions):

[INFO] org.jvnet.jaxb2_commons:jaxb2-basics-annotate:jar:1.0.3-SNAPSHOT
[INFO] +- org.jvnet.jaxb2_commons:jaxb2-basics-tools:jar:0.8.1:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- com.sun.xml.bind:jaxb-impl:jar:2.2.7:provided
[INFO] |  +- com.sun.xml.bind:jaxb-core:jar:2.2.7:provided
[INFO] |  |  +- javax.xml.bind:jaxb-api:jar:2.2.11:provided
[INFO] |  |  \- com.sun.istack:istack-commons-runtime:jar:2.16:provided
[INFO] |  \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.12:provided
[INFO] |     \- javax.xml.bind:jsr173_api:jar:1.0:provided
[INFO] +- com.sun.xml.bind:jaxb-xjc:jar:2.2.7:provided
[INFO] +- org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-testing:jar:0.9.0:test
[INFO] |  \- org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:jar:0.9.0:test
[INFO] |     +- org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-core:jar:0.9.0:test
[INFO] |     |  +- com.sun.org.apache.xml.internal:resolver:jar:20050927:test
[INFO] |     |  +- org.apache.maven:maven-plugin-api:jar:2.0.9:test
[INFO] |     |  +- org.sonatype.plexus:plexus-build-api:jar:0.0.7:test
[INFO] |     |  \- org.apache.maven:maven-project:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-settings:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-profile:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-model:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-artifact-manager:jar:2.0.9:test
[INFO] |     |     |  +- org.apache.maven:maven-repository-metadata:jar:2.0.9:test
[INFO] |     |     |  \- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:test
[INFO] |     |     +- org.apache.maven:maven-plugin-registry:jar:2.0.9:test
[INFO] |     |     \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:test
[INFO] |     |        \- classworlds:classworlds:jar:1.1-alpha-2:test
[INFO] |     +- org.codehaus.plexus:plexus-utils:jar:1.5.15:test
[INFO] |     +- org.jvnet.jaxb2.maven2:maven-jaxb22-plugin:jar:0.9.0:test
[INFO] |     \- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.2:test
[INFO] |        \- org.apache.maven:maven-artifact:jar:3.0:test
[INFO] +- org.jvnet.annox:annox:jar:1.0.1:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.2.1:compile
[INFO] |  +- commons-io:commons-io:jar:1.2:compile
[INFO] |  \- com.google.code.javaparser:javaparser:jar:1.0.11:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

Maven stuff is unrelated, otherwise you should be able to see the versions.

Hope this helps.

To be honest, I am a bit reluctant to invest time in batch/command-line sample. But if you'll get one working, I'd add it to samples.

Why do you actually need it in the command line?

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks for your replay. I never used Jaxb before. A colleague gave me jaxb-ri package, it's why I used it. Is there any tutorial to use the builder in eclipse ? Thank in advance. – Samy Nov 06 '15 at 16:33
  • @Samy A Maven project with `maven-jaxb2-plugin` in `pom.xml` works in Eclipse basically OOTB. – lexicore Nov 09 '15 at 10:28
  • 1
    Trying to get this working in Gradle but it's really kicking my buttocks. :-( – user447607 Jan 25 '16 at 14:54
  • @user447607 Really? Ask a question. I'm not much into Gradle but colleagues use this plugin in Gradle builds. – lexicore Jan 25 '16 at 21:46
  • @lexicore I finally got it working yesterday. Had to use just the right libraries, etc. Annox and CXF 3.1.4 wsdl2java generation don't seem to like each other much at all. Set 3.0.3 in Spring dependency management plugin. Probably should eliminate Annox annotations in favor of newer Java standard annotations anyway. – user447607 Jan 26 '16 at 14:59
  • @user447607 Yes, I'd recommend to use Java notation. It's still parsed by Annox, so the dependency will stay anyway. – lexicore Jan 26 '16 at 19:36