How do I set -Dfile.encoding within ant's build.xml?
Asked Answered
N

4

51

I've got java source files with iso-8859-1 encoding. When I run ant, I get "warning: unmappable character for encoding UTF-8". I can avoid this if I run ant -Dfile.encoding=iso-8859-1 or add encoding="ISO-8859-1" to each javac statement.

Is there a way to set the property globally within build.xml? <property name="file.encoding" value="ISO-8859-1"> does not work. I know that I can add a foo=ISO-8859-1 property and set encoding="${foo}" to each javac statement, but I'm trying to avoid that.

Neodymium answered 27/8, 2009 at 7:9 Comment(1)
The problem should be solved in four places simultaneously: https://mcmap.net/q/354685/-ant-encoding-problem-on-windows-utf-8-files-but-spits-garbage-on-diacriticsElutriate
A
38

A few options:

  1. add -Dfile.encoding=iso-8859-1 to your ANT_OPTS environment variable
  2. use <presetdef> to setup defaults for all of your <javac> invocations
Anatolio answered 27/8, 2009 at 7:37 Comment(0)
S
77

If you've got files encoded in a particular way, it's probably best to tell javac that rather than forcing the whole JVM to use a particular encoding. The javac task has an encoding attribute for this reason.

<javac srcdir="${src.dir}" destdir="${build.classes.dir}" encoding="iso-8859-1" />

But really, you should just convert the source files to UTF-8. Everything tastes better in UTF-8. :)

Submultiple answered 27/8, 2009 at 7:42 Comment(1)
Even if you convert the contents to UTF-8, you sometimes have to have a clean environment (LC_ALL=C) and there that comes pretty much handy.Hungerford
A
38

A few options:

  1. add -Dfile.encoding=iso-8859-1 to your ANT_OPTS environment variable
  2. use <presetdef> to setup defaults for all of your <javac> invocations
Anatolio answered 27/8, 2009 at 7:37 Comment(0)
B
3

Before changing build file i am getting java compile error like below.

ApplicationConstant.java:73: error: unmappable character for encoding ascii public static final String INVALID_MDTVERSION_SECOND = " This not compatible with the server’s Version";

I have encounter this error when I used to have ant java target as:

 <javac encoding="ascii"...>

Than i have change as below

<javac encoding="iso-8859-1" ...> 

This issue got solved.

Backsight answered 14/10, 2015 at 15:13 Comment(0)
F
1

Ant itself cannot set system properties, but if you really want to, you can write a java program which sets the system property, and execute that from within Ant.

Fimble answered 27/8, 2009 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.