0

I read lines from a .txt file into a String list. I show the text in a JTextPane. The encoding is fine when running from Eclipse or NetBeans, however if I create a jar, the encoding is not correct. The encoding of the file is UTF-8. Is there a way to solve this problem?

Daniel
  • 318
  • 6
  • 20

2 Answers2

1

Your problem is probably that you're opening a reader using the platform encoding.

You should manually specify the encoding whenever you convert between bytes and characters. If you know that the appropriate encoding is UTF-8 you can open a file thus:

FileInputStream inputFile = new FileInputStream(myFile);
try {
  FileReader reader = new FileReader(inputFile, "UTF-8");
  // Maybe buffer reader and do something with it.
} finally {
  inputFile.close();
}

Libraries like Guava can make this whole process easier..

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

Have you tried to run your jar as

java -Dfile.encoding=utf-8 -jar xxx.jar

Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24