“unmappable character for encoding” warning in Java
Question
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?
Solution
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.";
OTHER TIPS
Try with: javac -encoding ISO-8859-1 file_name.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>
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.
put this line in yor file .gradle above the Java conf.
apply plugin: 'java'
compileJava {options.encoding = "UTF-8"}
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
-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)
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"}
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>
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) ;
- Select Project
- Right click and select Properties
- Select Resource on Resource Panel(Top of right menu which opened after 2.)
- 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 = "İİİİİııııııççççç";
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.
If one is using Maven Build from the command prompt one can use the following command as well:
mvn -Dproject.build.sourceEncoding=UTF-8