【问题标题】:Ant Java build: Compilation issue - Change of character set from ISO-8859-1 to UTF-8Ant Java build:编译问题 - 将字符集从 ISO-8859-1 更改为 UTF-8
【发布时间】:2016-11-28 15:39:44
【问题描述】:

字符集编码格式有点新。我有一个 ant 构建脚本,它以 ISO-8859-1 格式编译我的 java 代码。它工作正常。

阅读几篇文章后: How do I convert between ISO-8859-1 and UTF-8 in Java?

我已将字符集格式更改为 UTF-8,从那时起编译问题就开始了。

抛出的错误是:

[javac] TestEncoding.java (at line 11)
[javac] case '?' :
[javac] ^^^^^^^^

我的构建脚本如下:

<javac compiler="org.eclipse.jdt.core.JDTCompilerAdapter"
destdir="bin" debug="true" deprecation="on" encoding="iso-8859-1"
source="1.6" target="1.6"
debuglevel="lines,source" failonerror="false" errorProperty="buildFailed">
<compilerarg line="-warn:+raw" />
<compilerarg line="-warn:-serial" />
<compilerarg line="-log source/testapp/compileLog.xml" />
<src path="testapp" />
<classpath refid="application.classpath" />
</javac>

我的一个有问题的班级中有以下代码:

public class TestEncoding {
public static final String filterAccent(String s) {
    StringBuffer sb = new StringBuffer();
    int n = s.length();

    for (int i = 0; i < n; i++) {
        char c = s.charAt(i);
        switch (c) {
        case 'á':
            sb.append("a");
            break;
        case 'à':
            sb.append("a");
            break;
        case 'ã':
            sb.append("a");
            break;
        case 'À':
            sb.append("A");
            break;
        case 'â':
            sb.append("a");
            break;
        case 'Â':
            sb.append("A");
            break;
        case 'ä':
            sb.append("a");
            break;
        case 'Ä':
            sb.append("A");
            break;
        case 'å':
            sb.append("a");
            break;
        case 'Å':
            sb.append("A");
            break;
        case 'ç':
            sb.append("c");
            break;
        case 'Ç':
            sb.append("C");
            break;
        case 'é':
            sb.append("e");
            break;
        case 'É':
            sb.append("E");
            break;
        case 'è':
            sb.append("e");
            break;
        case 'È':
            sb.append("E");
            break;
        case 'ê':
            sb.append("e");
            break;
        case 'Ê':
            sb.append("E");
            break;
        case 'ë':
            sb.append("e");
            break;
        case 'Ë':
            sb.append("E");
            break;
        case 'í':
            sb.append("i");
            break;
        case 'ì':
            sb.append("i");
            break;
        case 'ï':
            sb.append("i");
            break;
        case 'î':
            sb.append("i");
            break;
        case 'Ï':
            sb.append("I");
            break;
        default:
            sb.append(c);
            break;
        }
    }
    return sb.toString();
   }
}

我也尝试将字符集更改为 UTF-16,但这次却抛出了不同的错误:

build.xml:152: com.ibm.team.repository.common.validation.PropertyConstraintException: Validation errors for item: type = CompilePackage, itemId = [UUID _ORXiULV3Eea3M7KtSY0KHw]
    Value of attribute "compileSources.errors.sourceText" is 67854 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 58296 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 36105 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 127899 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 155844 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 120795 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 81561 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 33264 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 35163 bytes, which is greater than the allowed encoded length of 32768 bytes.
    Value of attribute "compileSources.errors.sourceText" is 96396 bytes, which is greater than the allowed encoded length of 32768 bytes.
    at com.ibm.team.repository.service.internal.RdbRepositoryDataMediator.failIfNecessary(RdbRepositoryDataMediator.java:456)
    at com.ibm.team.repository.service.internal.RdbRepositoryDataMediator.validateItem(RdbRepositoryDataMediator.java:405)

有人可以帮忙吗?

感谢和问候,

维杰·雷迪。

【问题讨论】:

  • 确保javac 任务的encoding 属性与源文件的真实编码匹配。
  • 我的源文件字符编码是 cp-1252。但是使用 ISO-8859-1 可以正常工作,而 UTF-8 则不行。
  • CP-1252 和 ISO-8859-1 是非常接近的编码,大多数字符的表示方式相同。尝试以 UTF-8 对源文件进行编码,并将 UTF-8 指定为encoding 属性。
  • @Berger 源代码在 RHEL 服务器上编译。在一篇文章中,我看到 linux 机器的默认编码是 UTF-8。我们有 ISO-8859-1 的等效编码吗?尝试您提到的选项是一个巨大的选择。作为一名构建工程师,我无法做到这一点,因为有许多具有相同编码的来源,我无法建议开发人员使用相同的选项。
  • 我没有得到您更改为 UTF8 的内容和方式,是您的源代码还是什么??

标签: java encoding utf-8 ant character-encoding


【解决方案1】:

我在编码级别尝试了多种方法。没有任何效果。

最后我尝试了 Berger 的建议,将源代码编码格式更改为 UTF-8,然后使用 UTF-8 构建,一切正常。我唯一需要注意的是项目中使用的特殊字符。一旦项目级别的编码发生更改,特殊字符就会更改为 ??符号。我需要转换所有这些??到实际的特殊字符。这是我唯一需要为此付出的努力。对于开发人员来说,这可能是一个混乱的情况,但由于这是开发人员/每个项目的一次性活动,这应该没问题。

感谢伯杰的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-08
    • 2011-07-26
    • 2013-01-03
    • 2011-09-10
    • 2016-10-04
    • 2020-01-25
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多