开发者

How to use UTF-8 character in Netbeans

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 I开发者_StackOverflow中文版DE or use some settings in my Java code?


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.


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.


You can check this resource for help on setting character encoding

FaqI18nProjectEncoding


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


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):

  • Can't print multi-byte character in the Output window with Java 18
  • Netbeans Output window uses wrong encoding
  • Output encoding in Windows uses cp-1252 by default instead of utf-8

This topic never grows old, it seems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜