How to specify character encoding for Ant Task par

2019-05-26 22:23发布

I'm writing an ANT task in Java.

In my build.xml I specify parameters, which should be read from my java class. Problems occur, when I use special characters, like german umlauts (Ö,Ä,Ü) in these parameters. In my java task they appear as ?-characters (using System.out.print from within eclipse).

All my files are encoded as UTF-8. and my build.xml has the corresponding declaration:

<?xml version="1.0" encoding="UTF-8" ?>

For the details of writing the task: I do it according to http://ant.apache.org/manual/develop.html (especially Point 5 nested elements). I have nested elements in my task like:

<parameter name="test"   value="ÖÄÜtest"/>

and a java method:

public void addConfiguredParameter(Parameter prop) {
    System.out.println(prop.getValue());
    //prints ???test
}

to read the parameter values.

3条回答
Juvenile、少年°
2楼-- · 2019-05-26 22:59

There are several transcoding operations going on here:

  1. Saving the XML as UTF-8 by your editor
    • Check that the characters are encoded correctly using a hex editor
  2. The parsing of the XML by Ant from UTF-8 to UTF-16 strings
    • A fault here is very unlikely
  3. Transcoding by the System.out PrintStream from UTF-16 strings to the platform encoding
    • Check that the encoding used supports the characters
  4. Decoding of the bytes received by the Eclipse console into UTF-16 strings for display
    • Check that the encoding used by the console matches that of the PrintStream

Encoded as UTF-8, you would expect the following encoded values in your XML file:

Grapheme  UTF-8 encoded bytes
Ö         c3 96
Ä         c3 84
Ü         c3 9c
查看更多
走好不送
3楼-- · 2019-05-26 22:59

The problem somehow vanished into thin air and was probably already fixed by switching everything to utf-8, but maybe eclipse didn't react so fast. Anyway I couldn't reproduce the error.

A problem which remained was, that when I referred to a build.properties file (which uses the characters mentioned) from my build.xml - then my java task still didn't get the characters right. But I could circumvent this by using \u and the hex representation of the letters - although that's not really convenient!

查看更多
我只想做你的唯一
4楼-- · 2019-05-26 23:09

Have you tried starting java with the following parameter?

-Dfile.encoding=UTF-8
查看更多
登录 后发表回答