wood burning stoves
The moose likes Java in General and the fly likes Encoding in Java file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encoding in Java file" Watch "Encoding in Java file" New topic

Encoding in Java file

Milly Biswas

Joined: Oct 05, 2011
Posts: 4
Hi All,

I am facing some strange output of the same compiled class, when running from eclipse and when my project is deployed in tomcat. My java file contains characters like "ÔĬĊúĺ". If I run the project from eclipse it works fine and the class file is also good. This is because the default encoding in eclipse is UTF-8. But when the project is deployed in tomcat server the values are garbaged. I am not sure why

I have the following in the pom.xml file, to ensure UTF-8 encoding:

Also for maven I specified:

Even after mentioning the enconding here does not seem to be enough. Can anyone help me with this?

Also I have been trying a lot of options after googling. Here they are:
1. Set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 in the environment variables.
2. Set URIEncoding="UTF-8" in server.xml in the tomcat\conf folder.

Nothing seems to work. The issue could be in JVM or in Tomcat, as to how they read the files... what default encoding is used.

So for testing the JVM. I have a java file 'HelloWorld' , like below:
import java.nio.charset.Charset;

public class HelloWorld {

public static void main(String[] args) {
String str = "Ô";
Charset charset = Charset.defaultCharset();
System.out.println("Default encoding: " + charset + " (Aliases: " + charset.aliases() + ")");



And I compile the file from command prompt (not using eclipse). And here is the result:

D:\TestFiles\myWork>"c:\Program Files\Java\jdk1.6.0_26\bin\javac.exe" -encoding
UTF8 HelloWorld.java

D:\TestFiles\myWork>"c:\Program Files\Java\jdk1.6.0_26\bin\java.exe" HelloWorld
Default encoding: windows-1252 (Aliases: [cp1252, cp5348])

So the default encoding of the JVM is windows-1252. Now if I try to set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 in the environment variables. Here is the result:

D:\TestFiles\myWork>"c:\Program Files\Java\jdk1.6.0_26\bin\javac.exe" -encoding
UTF8 HelloWorld.java
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

D:\TestFiles\myWork>"c:\Program Files\Java\jdk1.6.0_26\bin\java.exe" HelloWorld
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Default encoding: UTF-8 (Aliases: [UTF8, unicode-1-1-utf-8])

Even now the output is not the same as the java file. What am I missing here... ?? Any help would be great!


Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Blame the command prompt. It's very bad at displaying characters outside the (extended) ASCII range.

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Encoding in Java file
It's not a secret anymore!