• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encoding in Java file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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





 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Blame the command prompt. It's very bad at displaying characters outside the (extended) ASCII range.
reply
    Bookmark Topic Watch Topic
  • New Topic