File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encoding in Java file" Watch "Encoding in Java file" New topic
Author

Encoding in Java file

Milly Biswas
Greenhorn

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:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Also for maven I specified:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

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 = "Ô";
System.out.println("Hello");
System.out.println(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
Hello
È
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
Hello
Ô
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!

Thanks
Milly





Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encoding in Java file