What Is XJC (XML to Java Compiler)

Q

What Is XJC (XML to Java Compiler)?

✍: FYIcenter.com

A

What Is XJC (XML to Java Compiler) a command line tool provided in the JAXB API Reference Implementation package to help you to compile an XML schema file into Java data object classes according the JAXB specification.

If you want to use XJC, you can can download the JAXB API Reference Implementation package as shown in the previous tutorial at JAXB Gitbub Website.

For example, if you installed jaxb-ri.2.3.1.zip, you can run "xjc.sh" or "xjc.bat" from its "bin" sub-directory:

fyicenter$ ls -l /home/fyicenter/jaxb-ri/bin
  -rw-r--r--  4085 Sep 12  2018 xjc.bat
  -rwxr-xr-x  3922 Sep 12  2018 xjc.sh

Try the "xjc.sh -help" command:

fyicenter$ java -version 
  java version "15" 2020-09-15

fyicenter$ ../jaxb-ri/bin/xjc.sh -help
  Java major version: 1
  parsing a schema...
  ...

The xjc.sh script failed to detect the correct Java version number. Need to modify it.

fyicenter$ vi ../jaxb-ri/bin/xjc.sh
  ...
  JAVA_VERSION=`${JAVA} -version 2>&1 | head -n 1 | cut -d'"' -f2`
  ...

fyicenter$ ../jaxb-ri/bin/xjc.sh -help  
  Java major version: 15
  Error occurred during initialization of boot layer
  java.lang.module.FindException: Module java.activation not found, 
    required by com.sun.xml.bind

javax.activation.* is missing, since it is removed from JDK since Java 11. Need to download and add activation-1.1.1.jar from https://mvnrepository.com/artifact/javax.activation/activation/1.1.1. Also modify the script to use "-classpath ..." instead of "--module-path ...".

fyicenter$ vi ../jaxb-ri/bin/xjc.sh
  ...
  JAXB_PATH=${JAXB_HOME}/mod/jaxb-xjc.jar:\
    ...
    ${JAXB_HOME}/mod/relaxng-datatype.jar:\
    ${JAXB_HOME}/activation-1.1.1.jar
  
  ...
  exec "${JAVA}" -cp "${JAXB_PATH}" ${XJC_OPTS} com.sun.tools.xjc.XJCFacade "$@"
  ...

Try it again.

fyicenter$ ../jaxb-ri/bin/xjc.sh -help 
  Java major version: 15

Usage: xjc [-options ...] <schema file/URL/dir/jar> ... [-b <bindinfo>] ...
If dir is specified, all schema files in it will be compiled.
If jar is specified, /META-INF/sun-jaxb.episode binding file will be compiled.
Options:
  -nv                :  do not perform strict validation of the input schema(s)
  -extension         :  allow vendor extensions - do not strictly follow the
                        Compatibility Rules and App E.2 from the JAXB Spec
  -b <file/dir>      :  specify external bindings files (each <file> must have its own -b)
                        If a directory is given, **/*.xjb is searched
  -d <dir>           :  generated files will go into this directory
  -p <pkg>           :  specifies the target package
  -m <name>          :  generate module-info.java with given Java module name
  -httpproxy <proxy> :  set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost:proxyPort
  -httpproxyfile <f> :  Works like -httpproxy but takes the argument in a file to protect password 
  -classpath <arg>   :  specify where to find user class files
  -catalog <file>    :  specify catalog files to resolve external entity references
                        support TR9401, XCatalog, and OASIS XML Catalog format.
  -readOnly          :  generated files will be in read-only mode
  -npa               :  suppress generation of package level annotations (**/package-info.java)
  -no-header         :  suppress generation of a file header with timestamp
  -target (2.0|2.1)  :  behave like XJC 2.0 or 2.1 and generate code that doesnt use any 2.2 features.
  -encoding <encoding> :  specify character encoding for generated source files
  -enableIntrospection :  enable correct generation of Boolean getters/setters 
                          to enable Bean Introspection apis 
  -disableXmlSecurity  :  disables XML security features when parsing XML documents 
  -contentForWildcard  :  generates content property for types with multiple xs:any derived elements 
  -xmlschema         :  treat input as W3C XML Schema (default)
  -dtd               :  treat input as XML DTD (experimental,unsupported)
  -wsdl              :  treat input as WSDL and compile schemas inside it (experimental,unsupported)
  -verbose           :  be extra verbose
  -quiet             :  suppress compiler output
  -help              :  display this help message
  -version           :  display version information
  -fullversion       :  display full version information

Extensions:
  -Xinject-code      :  inject specified Java code fragments into the generated code
  -Xlocator          :  enable source location support for generated code
  -Xsync-methods     :  generate accessor methods with the 'synchronized' keyword
  -mark-generated    :  mark the generated code as @javax.annotation.Generated
  -episode <FILE>    :  generate the episode file for separate compilation
  -Xpropertyaccessors :  Use XmlAccessType PROPERTY instead of FIELD for generated classes

If you are using any version between JDK 6 and JDK 10, JAXB API Reference Implementation (including XJC) is included in the JDK package. You can run the "xjc" from its "bin" sub-directory:

fyicenter> \local\jdk-1.8.0\bin\xjc -version
  xjc 2.2.8-b130911.1802

fyicenter> \local\jdk-1.8.0\bin\xjc -help
  Usage: xjc [-options ...] <schema file/URL/dir/jar> ... [-b <bindinfo>] ...
  If dir is specified, all schema files in it will be compiled.
  If jar is specified, /META-INF/sun-jaxb.episode binding file will be compiled.
  ...

 

⇒ Generate Java Code from XML Schema with XJC

⇐ PurchaseOrder.java - JAXB Java Example Class

⇑ Examples for JAXB (Java Architecture for XML Binding)

⇑⇑ FAQ for jaxb-*.jar - Java Architecture for XML Binding

2018-05-19, 1550👍, 0💬