21

I'm trying to write Unicode characters (♠) using System.out, and a question mark (?) gets printed instead.

How can I have proper Unicode characters displayed instead of question marks?

I'm using IntelliJ IDEA on Windows, and trying to print from within the IDE.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Eran
  • 2,324
  • 3
  • 22
  • 27
  • See also [Intellij Idea incorrect encoding in console output](https://stackoverflow.com/questions/35231291/intellij-idea-incorrect-encoding-in-console-output) – Vadzim May 13 '20 at 15:04

8 Answers8

30

Go to Help > Edit Custom VM options... then add the following option:

-Dconsole.encoding=UTF-8
-Dfile.encoding=UTF-8

I'm not sure if both are necessary but it worked for me. You need to restart IntelliJ for changes to be applied.

I had already tried changing every encoding setting in Intellij, setting those options in Gradle and changing the system encoding, this is the only one that worked.

Nicolas
  • 6,611
  • 3
  • 29
  • 73
  • 2
    Nice one, thanks. Likewise, none of the other solutions worked. Worth adding that I needed to restart IntelliJ to get it to stick. Solutions elsewhere suggest editing the idea/idea64.exe.vmoptions file in IntelliJ's bin directory, but Help -> Edit Custom VM options opens one in ~\.IntelliJ$VERSION\config\idea64.exe.vmoptions. – Rogerborg Dec 03 '19 at 12:47
  • yeah I had a situation where I was running a batch program from a `nohup` on a linux terminal. The batch program would read from Solr and write to a text file. For some reason, all the UTF-8 characters like `–` were coming up as `?`. When I did `-Dfile.encoding=UTF-8` it fixed it – Nicholas DiPiazza Jun 22 '20 at 01:44
  • This works! Added in custom vm options. Restarted intelliJ. The Russian characters printed properly in console – Vignesh Apr 09 '21 at 18:58
18

A little update for the year 2015

TL;DR answer:

Go to Settings -> Editor -> File Encodings -> Project Encoding and set it to "UTF-8".

Expanded answer:

The reason why it does not work can be found by placing a breakpoint on a System.out.print() call. When the breakpoint hits, you can add System.out to Watches, and you can see that System.out.textOut.out.se.cs is set to windows-1252 or something similarly unsuitable.

The setting which magically worked for me (I do not know why) is in Settings -> Editor -> File Encodings -> Project Encoding. You need to set that to "UTF-8".

Then, unicode characters display properly on the console, and one more quick look with the debugger shows that the value of System.out.textOut.out.se.cs has magically turned into UTF-8.

I am saying "magically" because I do not see how and why an editor setting should affect the character set that System.out gets instantiated with when launching/debugging an application. If someone knows what is the logic behind this, please do tell!

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • 3
    2016.4: Doesn't work. Everything is UTF-8 including `System.out.textOut.out.se.cs` but I see boxed question marks (�) in the console. – Mark Jeronimus Sep 10 '16 at 15:16
  • @MarkJeronimus I was going to ask you to precisely describe your situation, but you might be better off starting a new question. You might want to link to this one to show that nothing here worked for you. – Mike Nakis Sep 10 '16 at 18:55
  • 1
    @MarkJeronimus your boxed question marks are an entirely different thing from regular question marks, and what they tell about your console is that it is perfectly capable of rendering unicode, so your problem may be elsewhere. – Mike Nakis Sep 10 '16 at 18:57
6

Is the file encoding configured correctly? See that "Settings | File Encodings" uses UTF-8. Printing ♠ works for me when I have IDE encoding and all files set to UTF-8. Recompiling may be needed after changing the encoding.

Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
2

System.out uses the default encoding of the underlying operating system which typically is ISO-8859-1 or UTF-8. The first is what I have with the 1252 codepage under XP.

Is this in a CMD.EXE window, or inside an IDE?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
1

If you ultimately want to print a wide range of Unicode characters on a standard command line on Windows, there is a bit of work involved. The default raster font will not support the characters and applications usually need to call the Unicode console API to render them. Java does not - it will first encode the characters to the native character set (a lossy process) and then emit them using an ANSI call. You can read this blog post if you want the gory details.

McDowell
  • 107,573
  • 31
  • 204
  • 267
1
  1. Do what @Mike Nakis said in his answer with going to Settings and changing project Encoding.

  2. In my case I had to do an additional step:

In the bottom right corner of IntelliJ IDEA switch encoding to UTF-8 enter image description here

Note that if you had some text pasted in your class with a different encoding (in my case I pasted a block of Cyrillic text right from the browser) it will become ugly formatted but that's ok. Just remove it and after switching to UTF-8 put it back.

Kirill Karmazin
  • 6,256
  • 2
  • 54
  • 42
0

The settings are not configured correctly, make sure that the your File encodings are set to UTF-8, in particular the 'Default encoding properties files'. On my side the steps below didnt work until I changed the default encoding.

On Intellij 2017.3.1 (Community Edition) File | Settings | Editor > File Encodings . Click on link below to see settings image

Intellij 2017.3.1 (Community Edition)

0

Replace your encoding.xml file which exists at .idea with the following:

<project version="4">
  <component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
    <file url="PROJECT" charset="UTF-8" />
  </component>
</project>```
Muhammad
  • 71
  • 1
  • 4