9

I am using Netbeans6.9.1 IDE and wants to show the Chinese characters in the output console using java. I copied the Chinese charater from a web page and copied between the "". but its not supported.

         String char1="世界你好";
         System.out.println(char1);

Do I need to do some setting in IDE or use some settings in my Java code?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
sjain
  • 1,635
  • 9
  • 25
  • 35
  • Did you ever find a solution for this? – theyuv Mar 09 '16 at 12:56
  • Related : [Netbeans console does not display Bangla unicode characters](http://stackoverflow.com/questions/7219249/netbeans-console-does-not-display-bangla-unicode-characters) – Quazi Irfan Mar 09 '16 at 14:34

5 Answers5

8

Edit the file netbeans.conf from the etc directory located at the installation directory of NetBeans. In the netbeans_default_options option, add -J-Dfile.encoding=UTF-8.

Works for both console input and output in NetBeans.

CephBirk
  • 6,422
  • 5
  • 56
  • 74
Mate Šimović
  • 945
  • 11
  • 11
  • 1
    I found that it is necessary for me to add -J-Dfile.encoding=UTF-8 in the netbeans.conf file and *also* make sure that my java program is being executed with the jre parameter -Dfile.encoding=UFT-8. I am using Maven to run my project. (Edit this under Properties/Run/VM Options.) This second step might only be necessary if you are using Maven. – Enwired Jan 06 '16 at 22:57
  • @Enwired I confirm the step you described solves some charset issues - NetBeans 12 – JRr Aug 13 '21 at 17:43
1

Can you try this instead:

String char1="\u4e16\u754c\u4f60\u597d";
System.out.println(char1);

The escape sequences get resolved by the javac compiler to the corresponding unicode codepoints, this way you are independent of the actual source code encoding. Any remaining display problems should then be caused by the console or an incomplete font.

PS: In my Netbeans installation (7.0 M2 on Ubuntu Linux) both strings mostly work except for the third character, which gets displayed as a box.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
1

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.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • netbeans 17 don't seem to have vm option – robert trudel Mar 17 '23 at 23:22
  • @roberttrudel - NetBeans 17 does have Run > VM Options if you are using a plain Java application. If you are using a Web Application, then no it does not - because in that case the VM options have to be added to whatever container you are using (Tomcat, Firefly, and so on...). Maybe that is why you are not seeing a VM option? Otherwise, this might make a good new question you could ask. – andrewJames Mar 17 '23 at 23:34
0

You have to see the character encoding used by your console. Mine works fine though. Another thing to check is the character encoding used by the class file u r implementing.

Project Properties | Sources | Encoding

Set it to UTF8

Timmo
  • 3,142
  • 4
  • 26
  • 43
  • This didn't work for me. I've been unable to get these characters to display within code in Netbeans. ✰☆★☞☛❸➂❤♡✉ My project is set as UTF8. Even "Courier New" didn't work for me, even though it does in Notepad++ with these same characters. I still haven't been able to figure this out. – Ryan May 27 '16 at 20:36
  • I've finally formally asked this as a question here: http://stackoverflow.com/questions/37644558/netbeans-not-displaying-utf8-unicode-characters-emoticons-correctly – Ryan Jun 05 '16 at 17:07
0

You can check this resource for help on setting character encoding

FaqI18nProjectEncoding

Flexo
  • 2,506
  • 3
  • 21
  • 32