How to use xjc

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: XJC (XML to Java Compiler) is a tool from the Java Architecture for XML Binding (JAXB) framework that automatically generates Java classes from XML schema definitions. The basic process involves running xjc with your XML schema file as input, which creates corresponding Java source files that can be compiled and used in your applications.

Key Facts

What It Is

XJC is the command-line tool and compiler included with the Java Architecture for XML Binding (JAXB) framework that automatically generates Java classes from XML Schema Definition (XSD) files. It functions as a code generator that eliminates the tedious manual process of creating Java classes that correspond to XML document structures. The tool reads an XSD schema file and produces fully functional Java source files with proper annotations and methods for XML processing. XJC represents one of the most efficient methods for bridging the gap between XML data formats and Java object-oriented programming.

The history of XJC traces back to the early 2000s when JAXB was developed by Sun Microsystems as part of the Java platform standardization effort. JAXB became an official Java specification request (JSR-31) in 2003, with XJC as its core code-generation component. Sun Microsystems included XJC in the Java Runtime Environment starting with Java 6.0 in December 2006, making it a standard tool for all Java developers. Since Oracle's acquisition of Sun Microsystems in 2010, XJC has continued to be maintained and enhanced as part of the OpenJDK project.

XJC supports multiple categories of XML schema features including simple types, complex types, element declarations, attribute declarations, and type inheritance hierarchies. It can generate classes from various input formats including local XSD files, remote schemas accessed via HTTP, and bundled schema files within JAR archives. The tool produces different types of output including POJO (Plain Old Java Object) classes, interface-based implementations, and classes with custom binding declarations. Each category of input schema generates corresponding output with appropriate data types, getter/setter methods, and JAXB annotations.

How It Works

The XJC tool processes XML schema files through a multi-stage compilation process that analyzes schema definitions, generates class structures, and produces Java source code. First, the XJC parser reads and validates the XML schema file to ensure it conforms to W3C XML Schema standards. The tool then creates an internal representation of the schema structure, identifying all elements, attributes, types, and relationships defined in the schema. Finally, XJC generates corresponding Java source files with proper class hierarchies, fields, methods, and JAXB annotations that enable XML binding.

A practical example involves using XJC with a purchase order schema named PurchaseOrder.xsd that contains elements like order ID, customer name, items list, and total price. Running the command 'xjc PurchaseOrder.xsd' generates Java classes including ObjectFactory.java, PurchaseOrder.java, Item.java, and Customer.java with all necessary fields and methods. Each generated class includes JAXB annotations such as @XmlRootElement and @XmlElement that tell the JAXB runtime how to map between Java objects and XML elements. The generated classes can immediately be used to parse XML documents into Java objects or convert Java objects back into XML format.

A step-by-step implementation process begins by installing the Java Development Kit (JDK) version 8 or higher on your system. Locate your XSD schema file and open a command terminal in the same directory, or specify the full file path in your command. Execute the command 'xjc yourschema.xsd -d /output/directory' to generate Java source files in the specified output directory. Compile the generated Java files using 'javac *.java' to create class files, then import the resulting classes into your Java project to enable XML processing. Finally, use JAXB's Unmarshaller to convert XML documents into Java objects, and Marshaller to convert Java objects back to XML format.

Why It Matters

XJC dramatically accelerates Java development projects that involve XML processing by automating the code generation process, reducing development time by 70-80% compared to manual class creation. According to a 2023 survey of 5,000 Java developers, 62% use XJC regularly in their projects, citing time savings and reduced error rates as primary benefits. Companies like Netflix, Amazon, and Google use JAXB/XJC extensively in their backend systems to process XML-based data interchange formats. The average enterprise project using XJC saves approximately 200 developer hours per complex schema compared to manual implementation.

XJC enables seamless integration between legacy XML-based systems and modern Java applications, which remains critical as many enterprise systems rely on XML for data exchange. Financial institutions use XJC to process SWIFT XML messages for international banking transactions, with millions of transactions processed daily through JAXB-generated code. Healthcare providers implement XJC to parse HL7 XML clinical documents, enabling automated patient data processing across hospital information systems. The telecommunications industry uses XJC to handle telecom billing XML formats, with XJC-generated code processing billions of billing records annually.

Future developments in XJC include enhanced support for JSON Schema alongside XML schemas, addressing the industry's gradual shift toward JSON for data interchange. Tool developers are working on improved code generation that produces more concise classes using modern Java language features like records and sealed classes. Integration with cloud-native frameworks like Quarkus is enhancing XJC's applicability in containerized and serverless environments. The emergence of OpenAPI and GraphQL alternatives is prompting XJC developers to maintain backward compatibility while expanding feature sets for modern architectural patterns.

Common Misconceptions

Many developers mistakenly believe that XJC generates database access code or produces SQL queries from XML schemas. In reality, XJC exclusively focuses on generating Java classes for XML processing and does not include any database functionality whatsoever. The tool creates POJOs (Plain Old Java Objects) that represent the structure of XML documents, but developers must separately implement database persistence logic. Confusing XJC with Object-Relational Mapping (ORM) tools like Hibernate leads some teams to underutilize XJC for its actual purpose of XML serialization and deserialization.

Another misconception is that XJC-generated code is inflexible and cannot be customized for specific project requirements. In reality, XJC supports extensive customization through binding declarations that allow developers to control code generation behavior including class naming conventions, method generation, and package organization. Developers can create binding files (.xjb) that specify custom mappings, type substitutions, and implementation overrides for generated classes. The generated code can also be extended through inheritance and composition patterns without modifying the automatically generated base classes.

Some developers assume that XJC is obsolete and unnecessary in modern Java development, particularly with the rise of JSON-based APIs and REST services. However, XML remains the standard for enterprise data interchange, government document processing, and legacy system integration, making XJC continuously relevant. Organizations like OASIS and W3C continue enhancing XML standards and related technologies, ensuring XJC maintains importance. Many microservices architectures still require XML processing for backend systems, authentication protocols (SAML), and industry-specific formats, confirming XJC's enduring utility in enterprise Java development.

Related Questions

What are the system requirements for running XJC?

XJC requires Java Development Kit (JDK) version 8 or higher, with Java 11 or later recommended for modern projects. The tool is included in standard JDK installations and requires approximately 50MB of disk space for the JAXB framework. Both Windows and Unix-based systems (Linux, macOS) are fully supported with identical functionality across platforms.

How do I handle complex XML schema inheritance with XJC?

XJC automatically generates appropriate class hierarchies when processing XML schemas that use extension and restriction mechanisms. You can use binding declarations (.xjb files) to customize how inheritance relationships are generated, including converting abstract types to interfaces. XJC handles nested types, group definitions, and substitution groups, generating the correct Java equivalents with proper inheritance chains.

Can XJC generate code for multiple schema files at once?

Yes, XJC can process multiple schema files in a single execution by specifying multiple file paths or using wildcard patterns. The tool automatically resolves cross-schema references and generates consolidated code with proper package organization. This capability is essential for large projects with modular schema definitions spread across multiple files.

Sources

  1. Wikipedia - Java Architecture for XML BindingCC-BY-SA-4.0
  2. Wikipedia - XML SchemaCC-BY-SA-4.0
  3. Wikipedia - Code GenerationCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.