0

I switched from SDK 16.0.2 to OpenJDK 20.0.1 and I can't see any special characters:

output characters

Instead of them I see "?" characters. I've tried change whole project Encoding to UTF-8 and still no success. I'd like to know what to do.

public class Main  {

public static void main(String[] args) {
    System.out.println("hello ěš  ");
}

}

Here is my file encoding:

file encoding

I tried to write into the console something with special characters, but I got a message full of question marks.

skomisa
  • 16,436
  • 7
  • 61
  • 102
jan kady
  • 1
  • 2
  • See where exactly? What are you doing? – aled Apr 22 '23 at 14:39
  • 1
    Please [*do not* use (sole) images of code/data/errors](https://meta.stackoverflow.com/a/285557/3439404) in your [mcve]. Copy the actual text, paste it into the question, then format it as code. – JosefZ Apr 22 '23 at 14:53
  • I mean in terminal I cant see the characters. – jan kady Apr 22 '23 at 15:49
  • Please check and update your question with the following settings: `Preferences | Editor | Color Scheme | Console Font`, `Preferences | Advanced Settings | Terminal -> Terminal character encoding`, `Preferences | Tools | Terminal -> Shell Path`. – Anthony Accioly Apr 22 '23 at 16:08
  • oh sorry my, bad its not terminal, but the RUN window in Intelij Idea – jan kady Apr 22 '23 at 21:28
  • This works fine for me using JDK 20 with Intellij IDEA 2023.1.1 Preview (Ultimate Edition). Are you saying that everything worked until you upgraded from 16 to 20? – skomisa Apr 23 '23 at 06:10
  • that's right, but when i switch back for example from the 20 to 16 then it works fine. – jan kady Apr 23 '23 at 07:45
  • Try changing your code to call `System.setOut(new PrintStream(System.out, true, "UTF8"));` before calling `println()`. That should fix the problem for Java releases >= 18. – skomisa Apr 24 '23 at 00:31
  • Try using the latest IntelliJ IDEA build from https://youtrack.jetbrains.com/articles/IDEA-A-2100661425/IntelliJ-IDEA-2023.1-Latest-Builds – Egor Klepikov Apr 24 '23 at 06:50
  • @EgorKlepikov I commented earlier that I couldn't reproduce the problem with 2023.1.1 Preview. However, my default charset is UTF-8. I think the issue only arises when `PrintStream.charset()` returns a value other than "UTF-8", so my confirmation didn't really prove anything. – skomisa Apr 24 '23 at 14:19
  • The issue is documented in JetBeans bug report [Running the Java project by using the JDK 18 prints the garbled characters in console when try printing the non-ASCII characters in the code](https://youtrack.jetbrains.com/issue/IDEA-291006/Running-the-Java-project-by-using-the-JDK-18-prints-the-garbled-characters-in-console-when-try-printing-the-non-ASCII-characters). In JDK 18+ the encoding used by `PrintStream` (which is the system default encoding unless specified otherwise) and the encoding used by the console must match. – skomisa Apr 24 '23 at 14:28

1 Answers1

0

Set the character encoding of your console to UTF-8. To change the default encoding of the console within IntelliJ, see the documentation.

Then read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I think the problem probably arose precisely because the console encoding was set to UTF-8, but their default charset was not UTF-8. This issue only arises with JDKs >= 18, which explains why everything worked for the OP with JDK 16. Based on a comment to the question, it looks like the latest Intellij IDEA release resolves the issue. – skomisa Apr 24 '23 at 14:24