简体   繁体   English

Netbeans 9 - 打印 Unicode 字符

[英]Netbeans 9 - Print Unicode Characters

With Netbeans 9:使用 Netbeans 9:

Product Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)
Java: 1.8.0_181; Java HotSpot(TM) 64-Bit Server VM 25.181-b13
Runtime: Java(TM) SE Runtime Environment 1.8.0_181-b13
System: Windows 10 version 10.0 running on amd64; UTF-8; en_EN (nb)

I want to be able to print:我希望能够打印:

String text = "你好!";
System.out.println(text);

The result is instead:结果是:

--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaApplication1 ---
???

I already added -J-Dfile.encoding=UTF-8 to the /etc/netbeans.conf , added also to the VM options in configuration.我已经将-J-Dfile.encoding=UTF-8添加到/etc/netbeans.conf ,也添加到配置中的 VM 选项。 Sources encoding option also set to UTF-8.源编码选项也设置为 UTF-8。 No problems with the past versions of Netbeans, here I found no way to display UTF-8 characters.过去版本的 Netbeans 没有问题,在这里我发现无法显示 UTF-8 字符。

Which way can I do?我可以用哪种方式?

For a Maven application created in NetBeans 9.0 using Java 8 there are three actions needed to get Chinese characters to render correctly in the Output window, the first two of which you were already doing:对于使用 Java 8 在 NetBeans 9.0 中创建的 Maven 应用程序,需要执行三个操作才能在“输出”窗口中正确呈现中文字符,您已经在执行前两个操作:

  1. Add -J-Dfile.encoding=UTF-8 to the property netbeans_default_options in file etc/netbeans.conf , and then restart NetBeans.-J-Dfile.encoding=UTF-8添加到文件etc/netbeans.conf 中的属性netbeans_default_options中,然后重新启动 NetBeans。
  2. From the Projects panel set {project} > Properties > Source > Encoding to UTF-8 .Projects面板设置{project} > Properties > Source > Encoding to UTF-8
  3. In the application call System.setOut(new PrintStream(System.out, true, "UTF8"));在应用程序中调用System.setOut(new PrintStream(System.out, true, "UTF8")); so that the print stream used when calling System.out.println() supports UTF-8 encoding.以便调用System.out.println()时使用的打印流支持 UTF-8 编码。

It's also worth noting some changes that are not necessary:还值得注意的是一些不必要的更改:

  • There's no need to select a specific font in the Output window ( Tools > Options > Miscellaneous > Output > Font ) since the default font of Monospaced works fine.无需在“输出”窗口(工具 > 选项 > 杂项 > 输出 > 字体)中选择特定字体,因为Monospaced的默认字体工作正常。 Selecting another font may actually cause problems if it does not support Chinese characters (eg Arial ).如果不支持中文字符(例如Arial ),选择另一种字体实际上可能会导致问题。
  • There's no need to specify file.encoding=UTF-8 in {project} > Properties > Run > VM Options .无需在{project} > Properties > Run > VM Options 中指定file.encoding=UTF-8
  • There's no need to specify anything about the project's encoding in pom.xml .无需在pom.xml 中指定有关项目编码的任何内容。

This is the code:这是代码:

package com.unthreading.mavenchinesechars;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class ChineseChars {

public static void main(String[] args) throws UnsupportedEncodingException {

    System.out.println("System.getProperty(\"file.encoding\"): " + System.getProperty("file.encoding"));
    System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset());
    System.out.println("System.getProperty(\"java.version\"): " + System.getProperty("java.version"));

    String text = "你好!"; 
    System.out.println(text); // <<<======================= Fails!       
    System.setOut(new PrintStream(System.out, true, "UTF8")); // Essential!
    System.out.println(text); // <<<======================= Works!       
}
}

This is pom.xml :这是pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.unthreading</groupId>
    <artifactId>MavenChineseChars</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

This is the Output in NetBeans:这是 NetBeans 中的输出

cd D:\NB82\MavenChineseChars; JAVA_HOME=C:\\Java\\jdk1.8.0_181 M2_HOME=C:\\apache-maven-3.6.0 cmd /c "\"\"C:\\apache-maven-3.6.0\\bin\\mvn.cmd\" -Dexec.args=\"-classpath %classpath com.unthreading.mavenchinesechars.ChineseChars\" -Dexec.executable=C:\\Java\\jdk1.8.0_181\\bin\\java.exe -Dmaven.ext.class.path=C:\\NetBeans9\\java\\maven-nblib\\netbeans-eventspy.jar org.codehaus.mojo:exec-maven-plugin:1.5.0:exec\""
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...

-----------------< com.unthreading:MavenChineseChars >------------------
Building MavenChineseChars 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------

--- exec-maven-plugin:1.5.0:exec (default-cli) @ MavenChineseChars ---
System.getProperty("file.encoding"): Cp1252
Charset.defaultCharset(): windows-1252
System.getProperty("java.version"): 1.8.0_181
???
你好!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  1.021 s
Finished at: 2018-12-12T18:24:12-05:00
------------------------------------------------------------------------

From the Output , note that:输出中,请注意:

  • The Chinese characters do not render correctly unless System.setOut(new PrintStream(System.out, true, "UTF8"));除非System.setOut(new PrintStream(System.out, true, "UTF8")); is called first.首先被调用。
  • The Chinese characters render even though System.getProperty("file.encoding") for the project returns "Cp1252" rather than "UTF-8" :即使System.getProperty("file.encoding")项目返回"Cp1252"而不是"UTF-8" ,中文字符也会呈现:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM