Generate Java class from XML Schema using JAXB ‘xjc’ command

14 May 2012 By Praveen Macherla 220,179 views 19 Comments
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

In the previous examples provided here and here, we saw how to generate XML Schema from Java classes using ‘schemagen’ command line tool and in Eclipse IDE. Now we will see how to do the reverse (i.e.) generate Java classes from XML Schema. This is done using JAXB binding compiler ‘xjc’ command.

Environment Used:

  • JDK 6 (Java SE 6) or later.
  • JAXB 2.1 API


Before using JAXB to create or access an XML document from Java application, we have to do the following steps:

  1. Binding the schema
    • Binding a schema means generating a set of Java classes that represents the schema for the XML document (Schema is not required for simple marshalling and unmarshalling).
  2. Marshal the content tree /Unmarshal the XML document.
    • After binding the schema, you can convert Java objects to and from XML document.

In this example we will see how to bind the schema. For that, we use Java Architecture for XML Binding (JAXB) binding compiler tool, xjc, to generate Java classes from XML schema.

ProcessJDK Tool
From XML schema -> Java source file(s) (i.e.) XSD -> Javaxjc

‘xjc’ Command Line Options


xjc [-options ...] … [-b ] …
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.

Complete list of options for ‘xjc’ is available in the help option.

xjc -help

Generate Java classes using ‘xjc’

Follow the steps below to generate a set of Java source files from XML schema.

  1. Create a new Java project folder and name it as “JAXBXJCTool”.
  2. Create a new XSD file and name it as “employee.xsd” and copy the following lines. This is the XML schema in our example which is to be bound to java classes.
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xs:schema version="1.0" xmlns:xs="">
      <xs:element name="employee" type="employee"/>
      <xs:complexType name="employee">
          <xs:element name="name" type="xs:string" minOccurs="0"/>
          <xs:element name="salary" type="xs:double"/>
          <xs:element name="designation" type="xs:string" minOccurs="0"/>
          <xs:element name="address" type="address" minOccurs="0"/>
        <xs:attribute name="id" type="xs:int" use="required"/>
      <xs:complexType name="address">
          <xs:element name="city" type="xs:string" minOccurs="0"/>
          <xs:element name="line1" type="xs:string" minOccurs="0"/>
          <xs:element name="line2" type="xs:string" minOccurs="0"/>
          <xs:element name="state" type="xs:string" minOccurs="0"/>
          <xs:element name="zipcode" type="xs:long"/>
  3. Save the file.
  4. Create a new folder ‘src’ inside the project folder.
  5. In Windows open Command Prompt (Windows button + R and type cmd) or Terminal in Linux and go to the project folder (use cd command) where it exists in your machine and type the following command.

    C:\Users\iByteCode\Desktop\JAXBXJCTool>xjc -d src -p com.theopentutorials.jaxb.beans employee.xsd

    This will generate set of Java source files with appropriate annotation inside ‘src’ folder.

The complete folder structure after executing the above command is shown below.

Tags: , , ,

  • pradip garala


  • JCP

    Thanks!!! Good Tutorial!

  • Venkat

    Nice Tutorial!!!

  • Adi

    Nice article.
    But I have a query.
    How to add these newly generated files into an eclipse project ?

  • Balaji

    getting error xjc is not recognized as an internal command

    • Bikram Kundu

      xjc.exe comes with jdk 1.6 and above. So first ensure you have installed jdk1.6 . Then set the Path (Sytem Variable) of your comp to the jdk1.6/bin folder. Once that is done open the command prompt once again and type xjc. It should work.

  • Balaji

    Finally i got, edited path variable up to my bin folder C:\program files\java version\bin

  • Navier

    How to use these generated classes for marshalling and unmarshalling?

  • Preethi

    Good tutorial. Thanks

  • Pingback: JAXB – Marshalling and Unmarshalling | My World Of Java

  • NPK

    Thanks a lot for showing light to others…this helped me i would like to know about JAXP too. thanks again

  • LM

    Thanks for sharing. Is there any way to get the source code that is parsing/compiling the xsd file into the java class?

  • masetti mani kanta

    Hi, when trying in command prompt I am getting an error, xjc is not recognized as an internal or external command.

    • sowhardh honnappa

      Install jdk 1.6 and above and make sure you change the directory to one where your jdk is installed, then typing xjc in cmd prompt should work

  • Pingback: Generate java classes using xjc | Coding Tech Utils

  • Pingback: Ссылки на статьи — Inoopolis STC

  • Pingback: Mapping XML Entities to Java Objects - QuestionFocus

  • Mahendra Kumar

    why it saying error, when i have given correct location.
    schema_reference.4: failed to read schema document ‘file:/D:/Mah/employee.xsd’ , because 1> could not find the document; 2> the document could not be read; 3> the root element of the document is not
    unknown location
    failed to parse a schema.

  • Mahendra Kumar