Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute the JAXB compiler from ANT

Tags:

java

jaxb

ant

jaxb2

I am using JAXB on a project. the attraction of JAXB is that it is bundled with the JDK, I have been to use xjc.exe on the command line to generate the .java files from a schema. I can't seem to find the JAXB ant task, sure there is a download at http://jaxb.java.net however i want to use the JAXB that is bundled into the JDK is there some way to call JAXB from ant what class does the xjc.exe call on?

like image 877
ams Avatar asked Sep 02 '10 19:09

ams


People also ask

When was JAXB removed from JDK?

Using the xjb and schemagen tools on JDK 11 The JAXB-specific xjc and schemagen tools, which you use to convert an XML Schema (*. xsd file) to a set of Java classes and vice versa, are included with the JDK up to version 10, but have been removed in JDK 11.

How do I run XJC?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

Why was JAXB removed?

JAXB was integrated within the JVM itself, so you didn't need to add any code dependency to have the functionality within your application. But with the modularization performed in JDK 9, it was removed as the maintainers wanted to have a smaller JDK distribution that only contains the core concepts.

Is JAXB available in Java 11?

As of Java 11, JAXB is not part of the JRE anymore and you need to configure the relevant libraries via your dependency management system, for example Maven or Gradle.


2 Answers

<target name="generate-jaxb-code">
    <java classname="com.sun.tools.internal.xjc.XJCFacade">
            <arg value="-p" />
            <arg value="com.example"/>
            <arg value="xsd/sample.xsd" />
    </java>
</target>

Just went hunting in the tools.jar and found the XJCFacade.class in com.sun.tools.internal tested the above code it works it produces the output as xjc.exe It seems that XJC.exe calls this code com.sun.tools.internal.xjc.XJCFacade

One of my key requirements was that the ant file had work within eclipse without having to include a path name to the JDK that way the file would be portable across operating systems. I am assuming that tools.jar is included on the classpath via the installed JRE preferences options.

like image 84
ams Avatar answered Sep 28 '22 02:09

ams


Here is a helpful link:

  • https://jaxb.java.net/nonav/2.0.2/docs/xjcTask.html

Java SE 6 does not ship the Ant task (see 7.1.3):

  • https://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html

Essentially they do the following:

<target name="xjc" description="....">
    <exec executable="${jdk.dir}/bin/xjc.exe">
        <arg value="-d"/>
        <arg value="${src.dir}"/>
        <arg value="-p"/>
        <arg value="com.mydomain.jaxb"/>
        <arg value="${etc.dir}/myschema.xsd"/>
    </exec>
</target>
like image 24
bdoughan Avatar answered Sep 28 '22 02:09

bdoughan