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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/464874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:58:57  来源:igfitidea点击:

"unmappable character for encoding" warning in Java

javaencodingutf-8ascii

提问by seanhodges

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?

如何将此字符注入“版权”字符串中,以便编译器满意,并且符号保留在文件中而没有潜在的重新编码问题?

采纳答案by Jon Skeet

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.";

回答by Fernando Nah

Try with: javac -encoding ISO-8859-1 file_name.java

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

回答by baybora.oren

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

P.S :this will ok if you static value in code. For Example String test = "????????????????";

PS:如果您在代码中使用静态值,就可以了。例如 String test = "?????????????????";

回答by Kelvin Goodson

I had the same problem, where the character index reported in the java error message was incorrect. 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.

我遇到了同样的问题,java错误消息中报告的字符索引不正确。在报告的位置是十六进制 094(取消而不是引号,但表示为引号)而不是十六进制 022 之前,我将其缩小到双引号字符。一旦我换成了十六进制 022 变体,一切都很好。

回答by Thomas Leonard

If you're using Maven, set the <encoding>explicitly in the compiler plugin's configuration, e.g.

如果您使用 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>

回答by nightlyop

This helped for me:

这对我有帮助:

All you need to do, is to specify a envirnoment variable called JAVA_TOOL_OPTIONS. If you set this variable to -Dfile.encoding=UTF8, everytime a JVM is started, it will pick up this information.

您需要做的就是指定一个名为 JAVA_TOOL_OPTIONS 的环境变量。如果将此变量设置为 -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/

回答by Alupotha

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
(File>settings>Java Compiler) add as additional command line parameter

并且您还可以将此编译选项添加到您的 IDE 中,例如:Intellij idea
(File>settings>Java Compiler) add as additional command line parameter

enter image description here

在此处输入图片说明

-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. (DOC)

-encoding :encoding 设置源文件编码名称,如EUC-JP和UTF-8。如果不指定-encoding,则使用平台默认转换器。(文件)

回答by 5122014009

If one is using Maven Build from the command prompt one can use the following command as well:

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

                    mvn -Dproject.build.sourceEncoding=UTF-8

回答by Alobes5

put this line in yor file .gradle above the Java conf.

将此行放在 Java conf 上方的 .gradle 文件中。

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

回答by Dxx0

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>