Here's the problem: I got some Chinese in my code, and I'm writing it into sqlite.
When I run my program in eclipse and read the string out of sqlite, it just works fine. But when I packaged the project to a jar and run it in command line. The Chinese string which is read out of sqlite is unreadable.
After some trials, I know that the problem lies in the file.encoding
system property. When I run the jar using command: java -Dfile.encoding=UTF-8 -jar TK.jar
, it works fine with Chinese word, But if I set the system property in code like: System.setProperty("file.encoding", "UTF-8");
, it won't work.
So, What is the difference between setting the system property from command line and code? And could anyone tell me how to set file.encoding
system property in code?
Thanks a lot!
To summary:
Remember to add charset when using String.getBytes()
as well as new String()
in order to avoid unreadable output from Chinese or Japanese when running program in different environment.
file.encoding
(not on the command line, nor in code). Somewhere in your code you are probably relying on the default character set being set toUTF-8
(for example, you might be callingString.getBytes()
without specifying the charset, or reading from a text file without specifying the charset). Fix that problem in your code. – SuspensoidSystem.getProperty
method to make sure it has already changed to "UTF-8" @Pammie – RedmondString.getBytes()
, Thanks a lot! (Could u issue your answer to below?) @Suspensoid – Redmond