简体   繁体   English

Java 中的“不可映射的编码字符”警告

[英]“unmappable character for encoding” warning in Java

I'm currently working on a Java project that is emitting the following warning when I compile:我目前正在开发一个 Java 项目,该项目在编译时发出以下警告:

/src/com/myco/apps/AppDBCore.java:439: warning: unmappable character for encoding UTF8
    [javac]         String copyright = "� 2003-2008 My Company. All rights reserved.";

I'm not sure how SO will render the character before the date, but it should be a copyright symbol, and is displayed in the warning as a question mark in a diamond.我不确定 SO 将如何在日期之前呈现角色,但它应该是版权符号,并在警告中显示为菱形中的问号。

It's worth noting that the character appears in the output artifact correctly, but the warnings are a nuisance and the file containing this class may one day be touched by a text editor that saves the encoding incorrectly...值得注意的是,该字符正确出现在输出工件中,但警告令人讨厌,并且包含此类的文件有一天可能会被错误地保存编码的文本编辑器触及......

How can I inject this character into the "copyright" string so that the compiler is happy, and the symbol is preserved in the file without potential re-encoding issues?如何将此字符注入“版权”字符串中,以便编译器满意,并且符号保留在文件中而没有潜在的重新编码问题?

尝试使用: javac -encoding ISO-8859-1 file_name.java

Use the "\\uxxxx" escape format.使用“\\uxxxx”转义格式。

According to Wikipedia , the copyright symbol is unicode U+00A9 so your line should read:根据Wikipedia ,版权符号是 unicode U+00A9 所以你的行应该是:

String copyright = "\u00a9 2003-2008 My Company. All rights reserved.";

If you're using Maven, set the <encoding> explicitly in the compiler plugin's configuration, eg如果您使用 Maven,请在编译器插件的配置中显式设置<encoding> ,例如

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

This helped for me:这对我有帮助:

All you need to do, is to specify a envirnoment variable called JAVA_TOOL_OPTIONS.您需要做的就是指定一个名为 JAVA_TOOL_OPTIONS 的环境变量。 If you set this variable to -Dfile.encoding=UTF8, everytime a JVM is started, it will pick up this information.如果将此变量设置为 -Dfile.encoding=UTF8,则每次启动 JVM 时,它都会获取此信息。

Source: http://whatiscomingtomyhead.wordpress.com/2012/01/02/get-rid-of-unmappable-character-for-encoding-cp1252-once-and-for-all/来源: http : //whatiscomingtomyhead.wordpress.com/2012/01/02/get-rid-of-unmappable-character-for-encoding-cp1252-once-and-for-all/

put this line in yor file .gradle above the Java conf.将此行放在 Java conf 上方的 .gradle 文件中。

apply plugin: 'java'
compileJava {options.encoding = "UTF-8"}   

Gradle Steps摇篮步骤

If you are using Gradle then you can find the line that applies the java plugin:如果您使用的是 Gradle,那么您可以找到应用 java 插件的行:

apply plugin: 'java'

Then set the encoding for the compile task to be UTF-8:然后将编译任务的编码设置为 UTF-8:

compileJava {options.encoding = "UTF-8"}   

If you have unit tests, then you probably want to compile those with UTF-8 too:如果您有单元测试,那么您可能也想用 UTF-8 编译它们:

compileTestJava {options.encoding = "UTF-8"}

Overall Gradle Example总体 Gradle 示例

This means that the overall gradle code would look something like this:这意味着整个 gradle 代码看起来像这样:

apply plugin: 'java'
compileJava {options.encoding = "UTF-8"}
compileTestJava {options.encoding = "UTF-8"}

Most of the time this compile error comes when unicode(UTF-8 encoded) file compiling大多数情况下,这个编译错误发生在 unicode(UTF-8 编码)文件编译时

javac -encoding UTF-8 HelloWorld.java

and also You can add this compile option to your IDE ex: Intellij idea并且您还可以将此编译选项添加到您的 IDE 中,例如:Intellij idea
(File>settings>Java Compiler) add as additional command line parameter (File>settings>Java Compiler) 添加为额外的命令行参数

在此处输入图片说明

-encoding : encoding Set the source file encoding name, such as EUC-JP and UTF-8.. If -encoding is not specified, the platform default converter is used. -encoding : encoding 设置源文件编码名称,如EUC-JP和UTF-8。如果不指定-encoding,则使用平台默认转换器。 ( DOC ) ( 文件)

This worked for me:这对我有用:

<?xml version="1.0" encoding="utf-8" ?>
<project name="test" default="compile">
    <target name="compile">
        <javac srcdir="src" destdir="classes" encoding="iso-8859-1" debug="true" />
    </target>
</project>

If you use eclipse (Eclipse can put utf8 code for you even you write utf8 character. You will see normal utf8 character when you programming but background will be utf8 code) ;如果你使用eclipse(Eclipse可以为你放utf8代码,即使你写utf8字符。你在编程时会看到正常的utf8字符,但后台会是utf8代码);

  1. Select Project选择项目
  2. Right click and select Properties右键单击并选择属性
  3. Select Resource on Resource Panel (Top of right menu which opened after 2.)资源面板上选择资源(在 2 之后打开的右侧菜单的顶部。)
  4. You can see in Resource Panel , Text File Encoding , select other which you want您可以在资源面板文本文件编码中看到,选择您想要的其他

PS : this will ok if you static value in code. PS:如果您在代码中使用静态值,就可以了。 For Example String test = "İİİİİııııııççççç";例如 String test = "İİİİİııııııççççç";

I had the same problem, where the character index reported in the java error message was incorrect.我遇到了同样的问题,java错误消息中报告的字符索引不正确。 I narrowed it down to the double quote characters just prior to the reported position being hex 094 (cancel instead of quote, but represented as a quote) instead of hex 022. As soon as I swapped for the hex 022 variant all was fine.在报告的位置是十六进制 094(取消而不是引号,但表示为引号)而不是十六进制 022 之前,我将其缩小到双引号字符。一旦我换成了十六进制 022 变体,一切都很好。

如果从命令提示符使用 Maven Build,也可以使用以下命令:

                    mvn -Dproject.build.sourceEncoding=UTF-8

For those wondering why this happens on some systems and not on others (with the same source, build parameters, and so on), check your LANG environment variable .对于那些想知道为什么在某些系统上而不是在其他系统上发生这种情况的人(具有相同的源、构建参数等),请检查您的LANG环境变量 I get the warning/error when LANG=C.UTF-8 , but not when LANG=en_US.UTF-8 .我在LANG=C.UTF-8时收到警告/错误,但在LANG=en_US.UTF-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM