0

I am working on a Java application which creates .docx files using Apache POI. However, "äöü" is not correctly displayed in the word file when I open it manually from the Intelij navigation after it was created. Do you have an idea what I might want to check/amend?

public void generateProtocolTEST() throws IOException {
    if (!Paths.get("./protocols").toFile().exists()) Files.createDirectories(Paths.get("./protocols"));
    XWPFDocument document = new XWPFDocument();
    
    FileOutputStream out = new FileOutputStream("protocols/" + "test.docx");

    document.createParagraph().createRun().setText("äüö");

    document.write(out);
    out.close();
}

Result IntelliJ settings

Solution:

  1. Added "[compileJava, compileTestJava].options.encoding = 'UTF-8'" to build.gradle file.
  2. Build the project via the gradle navigation again.
  3. Restarted the IDE.
Zuhlek
  • 3
  • 3
  • 3
    What happens with `setText("\u00e4\u00fc\u00f6");`? – g00se Aug 11 '21 at 14:30
  • 2
    What encoding have you set in IntelliJ in Settings -> Editor -> File Encodings? – Mario Varchmin Aug 11 '21 at 18:21
  • @g00se - with this i receive äüö! However, this seems a bit unconvenient... – Zuhlek Aug 12 '21 at 08:27
  • @Mario - I set all to UTF-8 (please find the screenshot of the settings added to my post). – Zuhlek Aug 12 '21 at 08:29
  • 1
    @Zuhlek: if what g00se suggested works, that means that the problem is how you compile your Java code! Tell the compiler/build tool/IDE what encoding to use and write your `.java` files with that encoding ([pick UTF-8](http://utf8everywhere.org/), it's just the correct choice for almost anything these days). – Joachim Sauer Aug 12 '21 at 08:49
  • ^^ What Joachim said. This is why I asked you that question – g00se Aug 12 '21 at 09:06
  • @Joachim & g00se - Thanks, understood. Nevertheless, I was a bit confused because I set everything to UTF-8 in the IDE (see screenshot in my question). I am really new to all those kind of settings, but for some reason the desired encoding had to be added in the build.gradle file as well (see answer). Please feel free to elaborate on this, very happy to learn :) In any case, have a great day! – Zuhlek Aug 12 '21 at 09:40
  • @Zuhlek: the settings in the screenshot are *the encoding used to save text files*. Setting that to UTF-8 is an important first step. But we don't know how you **compile** your code. If its within the IDE, then check under the Java settings what you've configured there. If it's from the command line, ensure you provide set [`-encoding utf8` in your `javac` invocation](https://stackoverflow.com/questions/9811382/compiling-javac-a-utf8-encoded-java-source-code-with-a-bom) and if it's from a build system (Gradle/Maven/Ant) check your build files. – Joachim Sauer Aug 12 '21 at 09:42

1 Answers1

1

If you didn't have UTF-8 set in IntelliJ before, you might have to recreate the .java file, which contains the code you show in your question (a restart of the IDE would probably also be a good idea), as changed encoding settings are effective for new files only.

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006974119-File-encoding-ignored-by-compiler

Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33
  • I see... Okay, I restarted Intelij after adding the following line in the build.gradle file as you linked thread suggested and it worked! Thank you :) [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' – Zuhlek Aug 12 '21 at 09:30