Jan 2023 - Update for newer versions of Java (17, 18, 19)
The approach in this answer, and its related comments recommends using the following:
- NetBeans config:
-J-Dfile.encoding=UTF-8
- Java VM option:
-Dfile.encoding=UTF-8
This has worked perfectly well for me in the past when using the NetBeans output window, with older versions of Java (e.g. Java 11 and 13) on older versions of NetBeans.
However, it no longer works for me, for Java 17 and later.
Instead you can follow these guidelines - with some caveats.
The caveats:
- I am using Windows (10 and 11) where the operating system default code page is Windows-1252. If you are using a different OS, your experience may be different.
- I am using NetBeans 16 (currently the most recent release).
- My tests were all with Eclipse Adoptium releases of Java (17, 18 and 19).
- I am specifically targeting the NetBeans output window (not a command line or terminal).
- My NetBeans output window is using the Monospaced 12 font (see NetBeans Options > Miscellaneous > Output). There's no point in using a font which may not support the Unicode characters you want to print - so don't forget to check that setting.
For Java 17
Double-check that Java 17 is actually being used in Project properties > Build > Compile > Java platform.
You need both of these:
- netbeans.conf (see below):
-J-Dsun.stdout.encoding=UTF-8
- Project properties > Run > VM Options:
-Dsun.stdout.encoding=UTF-8
And, optionally, if you are using Maven, you should set these Maven options:
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
This is similar to the answer I linked to above, except that instead of using:
file.encoding=UTF-8
you are now using:
sun.stdout.encoding=UTF-8
For Java 18
This is the same as Java 17, but with the Java platform (and, if relevant, Maven settings) changed accordingly, of course.
For Java 19
Instead of using this:
sun.stdout.encoding=UTF-8
Use this:
stdout.encoding=UTF-8
So, specifically, that means:
- netbeans.conf (see below):
-J-Dstdout.encoding=UTF-8
- Project properties > Run > VM Options:
-Dstdout.encoding=UTF-8
(And change your Maven settings if relevant.)
I think sun.stdout.encoding=UTF-8
still works but I expect stdout.encoding=UTF-8
is more appropriate, since sun
settings should generally be avoided (internal use only). I don't know if they work the same way, under the covers. According to the UTF-8 by Default JEP, sun.stdout.encoding
is "unspecified and unsupported".
I could not find any official documentation for the newer stdout.encoding
, except for a reference to it in this OpenJDK ticket. So, maybe an upcoming version of the Java Internationalization Guide will discuss it (but it's not mentioned in the current version of that guide).
Update - I found a reference to stdout.encoding
in the Java 19 JavaDoc for System.getProperties()
.
Don't Forget About STDERR
For Java 19, you can also add the following to your Project properties > Run > VM Options:
-Dstdout.encoding=UTF-8 -Dstderr.encoding=UTF-8
I don't know why, but you don't appear to need to add -J-Dstderr.encoding=UTF-8
to the netbeans.conf
file, as long as you do have -J-Dstdout.encoding=UTF-8
.
Where is netbeans.conf?
You can find it in a subdirectory where NetBeans is installed. On Windows, this is typically:
C:\Program Files\NetBeans-16\netbeans\etc\netbeans.conf
Make sure you add the -J-D...
option inside the closing double-quotes at the end of the (lengthy) netbeans_default_options="..."
string. Also be sure to include a space to separate this new option from the previous option in the string.
Always restart NetBeans after making a change to netbeans.conf.
The Windows Command Line
Already covered elsewhere, no doubt - but if you want to see UTF-8 output correctly in a Windows CMD shell, then you can use the Windows chcp
command:
chcp 65001
The default value (for me) is 437
- for Windows-1252 (also known as IBM437, and various other aliases).
You can see a list of identifiers here.
Further Details
You can see more detailed discussions in various NetBeans tickets, including (but not limited to):
This topic never grows old, it seems.