"unmappable character for encoding" warning in Java

JavaEncodingUtf 8Ascii

Java Problem Overview


I'm currently working on a Java project that is emitting the following warning when I compile:

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

I'm not sure how SO will render the character before the date, but it should be a copyright symbol, and is displayed in the warning as a question mark in a diamond.

It's worth noting that the character appears in the output artifact correctly, but the warnings are a nuisance and the file containing this class may one day be touched by a text editor that saves the encoding incorrectly...

How can I inject this character into the "copyright" string so that the compiler is happy, and the symbol is preserved in the file without potential re-encoding issues?

Java Solutions


Solution 1 - Java

Try with: javac -encoding ISO-8859-1 file_name.java

Solution 2 - Java

Use the "\uxxxx" escape format.

According to Wikipedia, the copyright symbol is unicode U+00A9 so your line should read:

String copyright = "\u00a9 2003-2008 My Company. All rights reserved.";

Solution 3 - Java

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>

Solution 4 - Java

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/

Solution 5 - Java

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

apply plugin: 'java'
compileJava {options.encoding = "UTF-8"}   

Solution 6 - Java

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)

Solution 7 - Java

###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"}

Solution 8 - Java

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>

Solution 9 - Java

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.

Solution 10 - Java

If you use eclipse (Eclipse can put utf8 code for you even you write utf8 character. You will see normal utf8 character when you programming but background will be utf8 code) ;

  1. Select Project
  2. Right click and select Properties
  3. Select Resource on Resource Panel(Top of right menu which opened after 2.)
  4. You can see in Resource Panel, Text File Encoding, select other which you want

P.S : this will ok if you static value in code. For Example String test = "İİİİİııııııççççç";

Solution 11 - Java

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.

Solution 12 - Java

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

                    mvn -Dproject.build.sourceEncoding=UTF-8

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
QuestionseanhodgesView Question on Stackoverflow
Solution 1 - JavaFernando NahView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaThomas LeonardView Answer on Stackoverflow
Solution 4 - JavanightlyopView Answer on Stackoverflow
Solution 5 - JavaAlobes5View Answer on Stackoverflow
Solution 6 - JavaAlupothaView Answer on Stackoverflow
Solution 7 - JavaLuke MachowskiView Answer on Stackoverflow
Solution 8 - JavaDxx0View Answer on Stackoverflow
Solution 9 - JavajakarView Answer on Stackoverflow
Solution 10 - Javabaybora.orenView Answer on Stackoverflow
Solution 11 - JavaKelvin GoodsonView Answer on Stackoverflow
Solution 12 - Java5122014009View Answer on Stackoverflow