Tuesday, November 08, 2005

jaxb, quick start guide

Here is a quick start from my experience:
  • Install jaxb: Remember jaxb will not come as a seperate installation. So finding a jaxb installation on java.sun is futile. Download and install the latest version of jwsdp. I have installed it here: "c:\Program Files\jwsdp-1.6". You can find jaxb in C:\Program Files\jwsdp-1.6\jaxb
  • The magical xjc compiler that does it all can be found in: C:\Program Files\jwsdp-1.6\jaxb\bin\xjc.bat
  • Always have the latest stable java build installed with you: C:\Program Files\Java\jdk1.5.0_04. Note JDK and not JRE! tools.jar is not found in jre.
  • Create a development/work folder something like c:\dev\jaxb. You will eventually have the following folder\file structure here:
    • c:\dev\jaxb
      • \lib (for supporting libs)
      • \work (for java files generate by the xjc compiler)
      • \classes (for class files generated by the javac compiler)
      • catalog.xsd (schema file to be persisted)
      • setEnv.bat
  • Copy the following files in dev\lib from the jaxb installation: jax-qname.jar, jaxb-api.jar, jaxb-impl.jar, jaxb-libs.jar, jaxb-xjc.jar, namespace.jar, relaxngDatatype.jar
  • Our generated schema classes will be kept under: dev\jaxb\classes
  • Create a setEnv.bat file. Mine looks like this:
  • @ECHO ON
    set CLASSPATH=.
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\classes
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\classes\myTest
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\jax-qname.jar
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\jaxb-api.jar
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\jaxb-impl.jar
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\jaxb-libs.jar
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\jaxb-xjc.jar
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\namespace.jar
    set CLASSPATH=%CLASSPATH%;C:\dev\jaxb\lib\relaxngDatatype.jar
  • Copy your favourite xsd file in the c:\dev\jaxb folder.
  • On command prompt execute the setEnv.bat file.
  • (Make sure you have things like jav\bin, ant\bin etc in the system path variable)
  • Once the env is set, you are all set.
  • To generate java files in teh work folder, run the following command:
    • xjc -p myTest catalog.xsd -d work
    • You just compilled your entire schema into .java classes with package structure myTest under the work folder. Nice!
  • To generate .class files from the generated .java files, use the following command line:
    • javac -d classes work\myTest\*.java
  • YOU ARE DONE!
  • Testing persistance:
    • change your directory to work folder. You should see the myTest package created here.
    • Now create a folder named: "client".
    • Copy the JAXBConstructor.java file from this zip.
    • Compile and run this file with a sample xml as input.


Very nice articles that gets you started with jaxb:
@link
@sun

0 Comments:

Post a Comment

<< Home