简体   繁体   English

错误:在maven编译期间编码UTF8的不可映射字符

[英]Error: unmappable character for encoding UTF8 during maven compilation

I am compiling a package using maven and it says build failure with following compilation error: 我正在使用maven编译一个包,它表示构建失败并出现以下编译错误:

SpanishTest.java[31, 81] unmappable character for encoding UTF8 SpanishTest.java [31,81]用于编码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. 我在线搜索,对于很多人来说,将源代码从UTF-8改为ISO-8859-1似乎可以工作,但我仍然得到相同的编译错误。 I am using 32-bit Ubuntu. 我使用的是32位Ubuntu。 Here is how that tag looks in my pom.xml 以下是该标记在我的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? 即使我将<project.build.outputEncoding>标记更改为ISO-8859-1,我仍然会收到错误。可能是因为java版本? I have both-sun java and openjdk installed on my system. 我的系统上安装了sun-sun和openjdk。

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 (eg): 配置maven-compiler-plugin以使用与源文件编码相同的字符编码(例如):

<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. 许多maven插件默认使用“project.build.sourceEncoding”属性,因此在你的pom中设置它将覆盖大多数插件。

<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. 当您的源代码由maven-compiler-plugin编译时,您的源代码文件将由编译器插件使用编译器插件配置的任何编码读入。 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. 许多人更喜欢将源文件的编码设置为UTF-8,以避免出现此问题。 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. 要在Eclipse中执行此操作,您可以右键单击项目并选择Properties-> Resource-> Text File Encoding并将其更改为UTF-8。 This will encode all your source files in UTF-8. 这将以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. (您还应该如上所述显式配置maven-compiler-plugin以使用UTF-8编码。)在源文件和编译器插件都使用相同的编码时,编译期间不应再有任何不可映射的字符。

Note, You can also set the file encoding globally in eclipse through Window->Preferences->General->Workspace->Text File Encoding. 注意,您还可以通过Window-> Preferences-> General-> Workspace-> Text File Encoding在eclipse中全局设置文件编码。 You can also set the encoding per file type through Window->Preferences->General->Content Types. 您还可以通过Window-> Preferences-> General-> Content Types设置每种文件的编码。

If the above answer does not work, change the encoding to cp1252 or manually remove all occurrences of the special character. 如果上述答案不起作用,请将编码更改为cp1252或手动删除所有出现的特殊字符。 For me special character was causing the prob which was inside a comment block. 对我来说, 特殊字符导致了评论块内的概率。

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

PS:I was using GNU/Linux OS(Ubuntu). PS:我使用的是GNU / Linux OS(Ubuntu)。

I came across this problem just now and ended up resolving it like so: I opened up the offending .java file in Notepad++ and from the Encoding menu I selected "Convert to UTF-8 without BOM". 我刚才遇到了这个问题并最终解决了它:我在Notepad ++中打开了违规的.java文件,并从编码菜单中选择了“转换为没有BOM的UTF-8”。 Saved. 保存。 Re-ran maven, all went through ok. 重新跑了maven,都经历好了。

If the offending resource was not encoded in UTF-8 - as you have configured for your maven compiler plugin - you would see in the Encoding menu of Np++ a bullet mark next to the file's current encoding (in my case I saw it was set to "Encode in ANSI"). 如果违规资源没有以UTF-8编码 - 正如您为maven编译器插件配置的那样 - 你会在Np ++的编码菜单中看到文件当前编码旁边的一个项目符号(在我的情况下,我看到它被设置为“用ANSI编码”)。

So your maven compiler plugin invoked the Java compiler with the -encoding option set to UTF-8, but the compiler encountered a ANSI-encoded source file and reported this as an error. 因此,您的maven编译器插件使用-encoding选项设置为UTF-8来调用Java编译器,但编译器遇到ANSI编码的源文件并将其报告为错误。 This used to be a warning previously in Java 5 but is treated as an error in Java 6+ 这在以前的Java 5中曾经是一个警告,但在Java 6+中被视为错误

This happens in the following scenario: When working on Windows, the IDE is more than likely configured to edit files in Cp1252, which is a Microsoft adaptation of latin-11. 在以下情形中会发生这种情况:在Windows上工作时,IDE很可能配置为编辑Cp1252中的文件,这是Microsoft改编的latin-11。 The developer checks in, and the Continuous Integration server (usually running on Linux, which nowadays is all utf8) picks up the file, and tries to compile as a UTF-8 file, hence the warning. 开发人员签入,持续集成服务器(通常在Linux上运行,现在全部是utf8)获取文件,并尝试编译为UTF-8文件,因此警告。

Try changing the encoding to cp1252 . 尝试将编码更改为cp1252 This works. 这有效。 To avoid future problems of this kind, use the same encoding on all the developer machines. 为避免此类问题,请在所有开发人员计算机上使用相同的编码。

Good luck... 祝好运...

In my case I resolved that problem using such approach: 就我而言,我使用这种方法解决了这个问题:

  1. Set new environment variable: JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8 设置新的环境变量: JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8
  2. Or set MAVEN_OPTS= -Dfile.encoding=UTF-8 或者设置MAVEN_OPTS= -Dfile.encoding=UTF-8

I too faced a similar issue and my resolution was different. 我也面临类似的问题,我的解决方案也不同。 I went to the line of code mentioned and traversed to the character (For SpanishTest.java[31, 81], go to 31st line and 81th character including spaces). 我去了提到的代码行并遍历了角色(For SpanishTest.java [31,81],转到第31行和第81个字符,包括空格)。 I observed an apostrophe in comment which was causing the issue. 我在评论中发现了一个引起问题的撇号。 Though not a mistake, the maven compiler reports issue and in my case it was possible to remove maven's 'illegal' character.. lol. 虽然不是一个错误,maven编译器报告问题,在我的情况下,有可能删除maven的“非法”字符..大声笑。

When i inspect the console i found that the version of maven compiler is 2.5.1 but in other side i try to build my project with maven 3.2.2.So after writting the exact version in pom.xml, it works good. 当我检查控制台时,我发现maven编译器的版本是2.5.1,但在另一方面,我尝试使用maven 3.2.2.So在pom.xml中写入确切的版本后构建我的项目,它运行良好。 Here is the full tag: 这是完整的标签:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.2</version>
  <configuration>
   ....
  <configuration>
</plugin>

Set incodign attribute in maven-compiler plugin work for me. 在maven-compiler插件中为我设置incodign属性。 The code example is the following 代码示例如下

<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>

I guess the issues happens at the encode strings. 我猜这些问题发生在编码字符串上。 I solved same issues. 我解决了同样的问题。 Please try adding trim() at last of the encode string. 请尝试在编码字符串的最后添加trim()。

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

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