19

I'm using tomcat 6.0.32 in windows 7. Inside a very simple servlet, inside the init method, I'm printing:

System.out.println(Charset.defaultCharset());

The output is:

windows-1253

As far as I understand this is how tomcat start's in the JVM since I'm printing the same in standalone java class and I get "UTF8" as a response.

So, how can I change this to UTF-8?

thanks

nonouco
  • 1,170
  • 1
  • 13
  • 25
  • did you try `request.getCharacterEncoding();` ? See what it prints, Anyway you can change the character encoding.. but it varies depends on the requirement. Have look at this wiki page http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q1 – RP- Jul 05 '12 at 11:21
  • I cant access request since I'm working inside the init method. In any case this is not a request-repsonse issue since I need to have UTF8 encoding in order to read files and not to send response in UTF8 encoding. – nonouco Jul 05 '12 at 11:31

4 Answers4

40

Just create a setenv.bat file inside the TOMCAT/bin directory with the following contents:

set "JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF8"
nonouco
  • 1,170
  • 1
  • 13
  • 25
  • Is the correct configuration -Dfile.encoding=UTF-8 or -Dfile.encoding=UTF8? I think that it should be UTF-8 – Marco Altieri Sep 24 '15 at 15:42
  • The correct is as described in the answer, i.e. UTF8 - check these two: 1) http://stackoverflow.com/questions/6031877/jvm-property-dfile-encoding-utf8-or-utf-8 2)http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html – nonouco Sep 25 '15 at 15:18
16

You may also add this setting in the Tomcat 7.0 windows service manager.

screenshot

from Dr James Bayley's blog

James Bayley
  • 233
  • 2
  • 7
  • This setting works for me.I have one more question is this setting requires only in windows os or we need to define this in linux also? – Yogesh Bombe Feb 26 '19 at 11:55
6

For Tomcat running as a Windows service neither %JAVA_OPTS% and neither setenv.bat answers works in my case.

I set the option "-Dfile.encoding=UTF8" in registry:

"HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\tomcatstackTomcat\Parameters\Java --> Options*

Then it works.

lpi314
  • 61
  • 1
  • 1
3

Try setting the (Windows) environment variable JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF-8 and start the Tomcat Server.

Will get more idea from this link: Setting the default Java character encoding

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66
  • Unfortunately this is not working. Setting -Dfile.encoding=UTF-8 is the correct approach but I don't thin that tomcat is appending JAVA_TOOL_OPTIONS to JAVA_OPTS. In any case I found the solution. Check below. – nonouco Jul 05 '12 at 11:38