Generate XML Schema from Java class using ‘schemagen’ tool

12 May 2012 By Praveen Macherla 36,195 views 3 Comments
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

Environment Used:

  • JDK 6 (Java SE 6) or later.
  • Any notepad or IDE for writing the Java code.
  • JAXB 2.1 API

JAXB Introduction

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects.

JAXB provides “xjc” tool to convert XML Schema to class representations. In addition, JAXB includes a “schemagen” tool which can essentially perform the inverse of “xjc”, creating an XML Schema from a set of annotated classes.

In this example, we will see how to generate XML schema from Java class using schemagen tool from command line.

What is an XML schema?

XML schema describes the structure of the data elements and their relationships in an XML document. XML schema can be written using Document Type Definition (DTD), XML Schema Language, and RELAX NG. Previous versions of JAXB reference implementation worked with only DTD and the latest versions uses XML Schema Language. The XML Schema language can also be referred as XML Schema Definition (XSD). We can create many XML document from a XML schema. For easy understanding we can say that,
XML schema ⇔ Java class.
With one Java class, we can create many objects similarly with one XML schema we can create many XML documents.

We use the Java Architecture for XML Binding (JAXB) schema generator tool, schemagen, to generate a XML schema from Java source files or class files.

ProcessJDK Tool
From Java source file -> XML schemaschemagen

‘schemagen’ Command Line Options


schemagen [-options ...]


-d Specify where to place processor and javac generated class files
-cp Specify where to find user specified files
-classpath Specify where to find user specified files
-helpDisplay this usage message
-episodeSpecify to generate an episode file for separate compilation.
-versionPrints the version information. If you specify this option, only the version information is output and typical command processing does not occur.

We are going to generate a XML schema for a XML document shown below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1">
    <designation>Junior Developer</designation>
        <line1>242 Main Street</line1>

Create required Java files

We create two Java classes Employee and Address with proper JAXB annotations set as follows.

package com.theopentutorials.jaxb.beans;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
public class Employee {
	private int id;
	private String name;
	private double salary;
	private String designation;
	private Address address;

	public int getId() {
		return id;

	public void setId(int id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public double getSalary() {
		return salary;

	public void setSalary(double salary) {
		this.salary = salary;

	public String getDesignation() {
		return designation;

	public void setDesignation(String designation) {
		this.designation = designation;

	public Address getAddress() {
		return address;

	public void setAddress(Address address) {
		this.address = address;

package com.theopentutorials.jaxb.beans;

public class Address {
	private String line1;
	private String line2;
	private String city;
	private String state;
	private long zipcode;
	public String getLine1() {
		return line1;
	public void setLine1(String line1) {
		this.line1 = line1;
	public String getLine2() {
		return line2;
	public void setLine2(String line2) {
		this.line2 = line2;
	public String getCity() {
		return city;
	public void setCity(String city) { = city;
	public String getState() {
		return state;
	public void setState(String state) {
		this.state = state;
	public long getZipcode() {
		return zipcode;
	public void setZipcode(long zipcode) {
		this.zipcode = zipcode;

‘schemagen’ command for schema genearation

In Windows open Command Prompt (Windows button + R and type cmd) or Terminal in Linux and go to the folder (use cd command) where your java code exists.

  • Compile the above Java classes and place the classes in ‘bin’ folder. Type the following command,

    C:\Users\iByteCode\Desktop\JAXBCodes\JAXBSchemaGen>javac -d bin src\com\theopentutorials\jaxb\beans\*.java

  • Now type the following command to generate the schema from Java source files.

    C:\Users\iByteCode\Desktop\JAXBCodes\JAXBSchemaGen>schemagen -cp bin src\com\theopentutorials\jaxb\beans\


Tags: , , , ,

  • Krishna

    The right command = C: emp>javac -d . *.java

  • Gareth Pitt-Nash

    Thank you for this, it’s a handy reference.

  • Mahesh Mhatre

    I am trying to use classpath option but it doesn’t seem to work. Can you add an example for user defined files be included in schemagen