Search code examples
javanetbeansencodingantjaxb

Change Jaxb Generated File Encoding Per project (non maven) in Netbeans


I have the following issue:

I have a non maven project in NetBeans 8.2 which has utf-8 encoding

(as shown with - right click on project properties).

In project I have created JaxB xml binding and so some sources are generated in generated folder.

However generated sources are not using utf-8 and so I got

error: unmappable character for encoding UTF-8

if I try to compile.

If I change netbeans_default_options in (netbeans etc\netbeans.conf) so that it contains the line -J-Dfile.encoding=UTF-8 the problem for the particular project is solved - everything compiles fine.

But this breaks the rest of my projects which are not utf-8.

I am looking for the solution that will not impact the rest of my projects.

Thanks.


Solution

  • The solution was adding the following

    <arg value="-encoding"/>
    <arg value="UTF-8"/>
    

    into the ant script inside xml_binding_build

    so that the resulting xjc would look like this

    <xjc package="mypackage" destdir="build/generated/jaxbCache/mypackage" catalog="catalog.xml">
                <classpath>
                    <pathelement location="${src.dir}"/>
                    <pathelement path="${jaxbwiz.xjcrun.classpath}"/>
                </classpath>
                <arg value="-xmlschema"/>
                <arg value="-encoding"/>
                <arg value="UTF-8"/>
                <schema file="xml-resources/jaxb/mypackage/mypackage.xsd"/>
                <depends file="xml-resources/jaxb/mypackage/mypackage.xsd"/>
                <produces dir="build/generated/jaxbCache/mypackage"/>
    </xjc>
    

    Ofcourse the xml_binding_build.xml is generated (automatically) and normally should not be edited by hand.

    However this workatround is enough for me and it is better than converting every project to utf-8.