Error: unmappable character for encoding UTF8 during maven compilation

JavaMavenCharacter Encoding

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

If the above answer does not work, change the encoding to cp1252 or manually remove all occurrences of the special character. 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).

Solution 3 - Java

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". Saved. Re-ran maven, all went through ok.

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

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. This used to be a warning previously in Java 5 but is treated as an error in Java 6+

Solution 4 - Java

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

Try changing the encoding to cp1252. This works. To avoid future problems of this kind, use the same encoding on all the developer machines.

Good luck...

Solution 5 - Java

In my case I resolved that problem using such approach:

  1. Set new environment variable: JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8
  2. Or set MAVEN_OPTS= -Dfile.encoding=UTF-8

Solution 6 - Java

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

Solution 7 - Java

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. Here is the full tag:

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

Solution 8 - Java

Set incodign attribute in maven-compiler plugin work for me. 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>

Solution 9 - Java

I guess the issues happens at the encode strings. I solved same issues. Please try adding trim() at last of the encode string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYogeshView Question on Stackoverflow
Solution 1 - JavaBenjaminLinusView Answer on Stackoverflow
Solution 2 - Javafalse9strikerView Answer on Stackoverflow
Solution 3 - JavaPeter PerháčView Answer on Stackoverflow
Solution 4 - JavaJamesView Answer on Stackoverflow
Solution 5 - JavaBSeitkazinView Answer on Stackoverflow
Solution 6 - JavaSiddhartha TripathyView Answer on Stackoverflow
Solution 7 - JavaTinyOSView Answer on Stackoverflow
Solution 8 - JavaJorge Santos NeillView Answer on Stackoverflow
Solution 9 - JavaMichelleView Answer on Stackoverflow