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.