views:

297

answers:

2

Hi, I finally wrote me little app. It's desktop app but it has embedded web server. When I lunched it from NetBeans everything is ok. When I lunch dist jar I have correct character encoding in GUI, but web server output is corrupted ("?" instead of national characters).

I use NetBeans 6.7.1, jdk1.6.0_16, http server from Java 6 SE and lib Rome 1.0

I don't put any source code here, because I have no idea witch part should I put.

//edit: data are hardcoded in Strings. Those Strings are passed to Rome as arguments to create RSS nodes, Romes RSS feeds are are written to String and then Strings are passed to HttpHandler.

+2  A: 

This issue probably has nothing to do with NetBeans. Usually character encoding issues are due to not defining the character encoding somewhere, in which case the actual character encoding will be determined pretty much by luck.

For instance, Java Strings are UTF-16 internally, but the encoding used by Java Readers is determined by the platform default unless explicitly specified.

Joonas Pulakka
Ok I understood, but what cause the different behavior of program runn from jar from dist folder and program run from NetBeans? Why i have more luck withe NetBeans?
Maciek Sawicki
How do you start NetBeans, and how do you run the jar - from command line, or by double-clicking? All these kind of things can affect what the JVM thinks is the "default encoding", and therefore relying on that "default encoding" (that is, not defining the encoding explicitly) is guaranteed to result in nondeterministic behavior.
Joonas Pulakka
I running jar by double-clicking and NetBeans by shortcut from desktop. I use Vista 64 Business SP2 English. I also checked jar (double clicking) on Win XP Pro English with the same result ("?" instead of Polish letters).
Maciek Sawicki
I'm not sure if NetBeans has an effect on the "default encoding" of the JVM it starts to run your program. Has or not, I'm pretty sure that the only effective cure is to check your program throughout. How are the Strings passed to HttpHandler? It seems that HttpHandler uses Streams, not Strings, so you must be converting the Strings to Streams somewhere. And streams allow to set the encoding...
Joonas Pulakka
thank You. changing `os.write(response.getBytes());` to `os.write(response.getBytes("utf-16"));` helped
Maciek Sawicki
+2  A: 
  • Check the encoding in the source files.
  • Check any point where encoding/decoding is performed (often any place where String -> byte[] or byte[] -> String). Anything that converts bytes to Strings is performing an encoding operation myEncoding -> UTF-16.
  • Check that you are passing the appropriate encoding information to 3rd party libraries that perform encoding/decoding.
  • If generating XML, ensure that the header encoding matches the encoding used to write the bytes (<?xml version="1.0" encoding="UTF-8"?>).
  • If serving content over HTTP, ensure that the content type and charset header is correct (e.g. Content-Type: text/html; charset=utf-8). A charset is usually only applicable if serving a text MIME type (it is not applicable for application/rss+xml, for example). Check your MIME documentation.
McDowell
Thank You. Nice encoding troubleshooting manual.
Maciek Sawicki