Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 730
Yogesh Avatar asked Jan 23 '12 20:01

Yogesh


2 Answers

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.

like image 84
BenjaminLinus Avatar answered Sep 18 '22 17:09

BenjaminLinus


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

like image 36
false9striker Avatar answered Sep 16 '22 17:09

false9striker