2

I have the following Java code to create an HTML file. I use the j2html library for that:

String html = html(
                     head(
                         meta().attr("charset", "UTF-8"),
                         title("test"),
                         link().withRel("stylesheet").withHref("style.css")
                     ),
                     body(
                            h1("Räte"),
                            h2("Äpfel")
                     )
                   ).render();
File file = new File("index.html");
FileUtils.writeStringToFile(file, html);

This works perfectly if I launch the program via IntelliJ, and I get this output:

test

Räte

Äpfel

But if I create a JAR artifact and launch it, the umlauts aren't displayed correctly. It looks like this:

test

R�te

�pfel

It's the same Java code and the charset is set to UTF-8.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
randomdev8712
  • 261
  • 1
  • 6
  • 23
  • Are you sure that the compiler also uses UTF-8? – ElectronicManuel Jul 28 '17 at 13:15
  • IntelliJ's console's default char encoding is whatever you have set in File Encodings page of the Settings dialog. Most likely UTF-8. That's why it display's correctly in your first scenario. In the second scenario I am assuming you are running the jar in bash or cmd.exe or similar. That program's character encoding would seem to be iso-9959-1 which is why it displays incorrectly, it is utf-8 encoded. When you change the encoding of the html to 8859 intellij can't make sense of it by bash/cmd can. See [this](https://www.jetbrains.com/help/idea/configuring-output-encoding.html) – JJF Jul 28 '17 at 13:56
  • After a little research it would appear bash uses utf-8 encoding by default so I'm guessing you're running cmd.exe on Windows? If so it would appear it's not possible to change cmd.exe's char encoding to utf-8. – JJF Jul 28 '17 at 14:03

1 Answers1

1

FileUtils.writeStringToFile(File, String) uses the JVM's default encoding when writing the string to the file.

The JVM's default encoding can differ depending on how you launch the JVM (that's why you've received different result from IntelliJ and direct JAR execution).

For this reason, the FileUtils.writeStringToFile(File, String) method is deprecated, and you should always use the FileUtils.writeStringToFile(File, String, Charset) method, where you specifify the encoding explicitly rather than relying on the JVM default:

FileUtils.writeStringToFile(file, html, StandardCharsets.UTF_8);
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103