125

我目前正在开发一个 Java 项目,该项目在编译时发出以下警告:

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

我不确定 SO 将如何在日期之前呈现字符,但它应该是版权符号,并在警告中显示为菱形中的问号。

值得注意的是,该字符正确显示在输出工件中,但是警告很麻烦,并且有朝一日,包含此类的文件可能会被错误地保存编码的文本编辑器触及...

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

4

12 回答 12

99

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

于 2009-10-24T20:59:58.660 回答
58

使用“\uxxxx”转义格式。

根据Wikipedia,版权符号是 unicode U+00A9 所以你的行应该是:

String copyright = "\u00a9 2003-2008 My Company. All rights reserved.";
于 2009-01-21T11:20:51.037 回答
46

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

<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>
于 2012-05-28T14:27:35.403 回答
34

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.

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

于 2014-02-13T13:28:49.323 回答
29

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

apply plugin: 'java'
compileJava {options.encoding = "UTF-8"}   
于 2015-09-13T23:03:22.053 回答
11

Most of the time this compile error comes when unicode(UTF-8 encoded) file compiling

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

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)

于 2015-04-05T06:17:35.030 回答
11

Gradle Steps

If you are using Gradle then you can find the line that applies the java plugin:

apply plugin: 'java'

Then set the encoding for the compile task to be UTF-8:

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

If you have unit tests, then you probably want to compile those with UTF-8 too:

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

Overall Gradle Example

This means that the overall gradle code would look something like this:

apply plugin: 'java'
compileJava {options.encoding = "UTF-8"}
compileTestJava {options.encoding = "UTF-8"}
于 2018-10-21T10:42:44.723 回答
4

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>
于 2017-04-30T01:53:52.040 回答
2

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. I get the warning/error when LANG=C.UTF-8, but not when LANG=en_US.UTF-8.

于 2020-04-14T19:07:30.717 回答
1

如果您使用eclipse(即使您编写utf8字符,Eclipse也可以为您放置utf8代码。您在编程时会看到正常的utf8字符,但背景将是utf8代码);

  1. 选择项目
  2. 右键单击并选择属性
  3. 资源面板上选择资源(在 2 之后打开的右上方菜单。)
  4. 您可以在资源面板文本文件编码中看到,选择其他您想要的

PS:如果您在代码中使用静态值,则可以。例如 String test = "İİİİİİıııııııçççççç";

于 2009-12-07T07:56:16.383 回答
1

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.

于 2010-06-22T12:14:13.167 回答
1

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

                    mvn -Dproject.build.sourceEncoding=UTF-8
于 2015-06-24T15:06:15.640 回答