错误:在 maven 编译期间编码 UTF8 的不可映射字符 [英] Error: unmappable character for encoding UTF8 during maven compilation

查看:68
本文介绍了错误:在 maven 编译期间编码 UTF8 的不可映射字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am compiling a package using maven and it says build failure with following compilation error:

SpanishTest.java[31, 81] unmappable character for encoding UTF8

I searched online and for many people, changing source encoding from UTF-8 to ISO-8859-1 seems to work but I am still getting same compilation error. I am using 32-bit Ubuntu. Here is how that tag looks in my pom.xml

<project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>

Even if I change <project.build.outputEncoding> tag to ISO-8859-1, I still get the error.Could it be because of java version? I have both-sun java and openjdk installed on my system.

Can anyone please let me know what to do.

Thanks

解决方案

Configure the maven-compiler-plugin to use the same character encoding that your source files are encoded in (e.g):

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

Many maven plugins will by default use the "project.build.sourceEncoding" property so setting this in your pom will cover most plugins.

<project>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
...

However, I prefer setting the encoding in each plugin's configuration that supports it as I like to be explicit.

When your source code is compiled by the maven-compiler-plugin your source code files are read in by the compiler plugin using whatever encoding the compiler plugin is configured with. If your source files have a different encoding than the compiler plugin is using then it is possible that some characters may not exist in both encodings.

Many people prefer to set the encoding on their source files to UTF-8 so as to avoid this problem. To do this in Eclipse you can right click on a project and select Properties->Resource->Text File Encoding and change it to UTF-8. This will encode all your source files in UTF-8. (You should also explicitly configure the maven-compiler-plugin as mentioned above to use UTF-8 encoding.) With your source files and the compiler plugin both using the same encoding you shouldn't have any more unmappable characters during compilation.

Note, You can also set the file encoding globally in eclipse through Window->Preferences->General->Workspace->Text File Encoding. You can also set the encoding per file type through Window->Preferences->General->Content Types.

这篇关于错误:在 maven 编译期间编码 UTF8 的不可映射字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆