Generate Java class from XML Schema in Eclipse IDE

In the example provided here, we saw how to generate Java classes from XML schema using JAXB binding compiler ‘xjc’ command. In this tutorial we will see how to generate the same in Eclipse IDE with the help of MOXy JAXB implementation which is available as part of Eclipselink project.

Environment Used:

  • JDK 6 (Java SE 6) or later.
  • Eclipse Indigo IDE for Java EE Developers (3.7.1).
  • EclipseLink 2.3.2 (Download EclipseLink from here and extract the zip file). We need this for MOXy which is an implementation of JAXB API.

Generate Java classes in Eclipse

Follow the steps below to generate Java source files from XML Schema in Eclipse IDE.

Creating JAXB project

  1. Open Eclipse IDE and create a new JAXB project which can be done in many ways,
    • File menu -> New -> Other -> JAXB -> JAXB Project
    • Click on the down arrow on New icon on toolbar -> Other -> JAXB -> JAXB Project
    • Right click on Project Explorer -> New -> Other -> JAXB -> JAXB Project


    Select JAXB Project and click Next.

  2. Enter the project name as “XJCJAXBPjt” and make sure jre6 is selected as the Target runtime with JAXB version 2.1.

    NOTE: for JRE version 6, the JAXB version is 2.1. If you are using JRE7 then you can use JAXB 2.2.

  3. Click Next -> Next.
  4. In JAXB Facet window, select “Generic JAXB 2.1” as platform with JRE as JAXB implementation as shown below and click Finish.

Create XSD file

Right click on ‘src’ and create a new package and name it as “com.theopentutorials.jaxb.beans”. Now create a new XSD file in the package created above. To create a new XSD file, Right click on package -> New -> Other…-> XML -> XML Schema File and click Next.

Select the location and enter the file name as ‘employeeSchema.xsd’. Copy the following code in XSD file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="employee" type="employee"/>

  <xs:complexType name="address">
    <xs:sequence>
      <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"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="employee">
    <xs:sequence>
      <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:sequence>
    <xs:attribute name="id" type="xs:int" use="required"/>
  </xs:complexType>
</xs:schema>

Adding JAR files

We need to add MOXy JAXB implementation JAR files in the project classpath for generating Java classes from schema. Follow the steps below to do the same.

Right click on your JAXB Project->Properties, select Java Build Path from left side pane and select Libraries tab from right side and click on Add External JARs. Add the following jars.

JAR File Location
com.sun.tools.xjc.[version number].jar [DOWNLOADED_ECLIPSELINK_FOLDER]/jlib/moxy
com.sun.xml.bind.[version number].jar [DOWNLOADED_ECLIPSELINK_FOLDER]/jlib/moxy
javax.xml.bind.[version number].jar [DOWNLOADED_ECLIPSELINK_FOLDER]/jlib/moxy

Generating Java classes from Schema

  • Right click on your package -> New -> Other… -> JAXB -> JAXB Classes from Schema.
  • Click Next.
  • Specify project for Java classes and click Next.
  • Select schema to generate classes from and click Next.
  • Enter the package name “com.theopentutorials.jaxb.beans” for the generated Java classes and Click Next.
  • In the JAXB compiler options window and vendor extensions window, keep the default options and click Next
  • Click Finish.

You will see the following message displayed in the console.

parsing a schema…
compiling a schema…
com\theopentutorials\jaxb\beans\Address.java
com\theopentutorials\jaxb\beans\Employee.java
com\theopentutorials\jaxb\beans\ObjectFactory.java

The complete folder structure for this example is shown below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.