martedì 20 marzo 2012

JAXB-XJC: java.lang.IllegalArgumentException: Illegal pattern character 'o' at java.text.SimpleDateFormat.compile

When using xjc compiler (jaxb version 2.2.5) to generate java classes from xsd files class generation may fail with some error like this:

 Illegal pattern character 'o' at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769) at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576) .....
(characted may be different - es. 'g' or other).
What is the problem?
Problem seems to be your locale setting because jaxb-xcj doen't support  locale different from US.

Solution 
Generate your classes with ant and introduce a javascript function to change your locale to US.

Sample ant file 

<taskdef
name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath>
<fileset dir="${basedir}/lib">
<include name="jaxb*.jar" />
<include name="xjc-*.jar" />
</fileset>
</classpath></taskdef>
<scriptdef
importClass(java.util.Locale);
actualDefault = Locale.getDefault();
project.setProperty("actual-default-locale", actualDefault);
Locale.setDefault(Locale.US);
]]>
name="setLocale" language="javascript"><![CDATA[
</scriptdef>

<target name="gen-oci-as" description="generates java classes from the OCISchemaAS.xsd">
<setLocale />
<xjc schema="${basedir}/xsd/OCISchemaAS.xsd" destdir="${basedir}/src" extension="true" removeOldOutput="true">
<produces dir="${basedir}/src" includes="**/*.java" />
<arg value="-verbose" />
<arg value="-npa" />
</xjc>
</target>

Nessun commento:

Posta un commento