Netbeans 9 - 打印 Unicode 字符 [英] Netbeans 9 - Print Unicode Characters

查看:40
本文介绍了Netbeans 9 - 打印 Unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Netbeans 9:

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

我希望能够打印:

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

结果是:

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

我已经将 -J-Dfile.encoding=UTF-8 添加到 /etc/netbeans.conf 中,也添加到配置中的 VM 选项中.源编码选项也设置为 UTF-8.过去版本的 Netbeans 没有问题,这里我发现无法显示 UTF-8 字符.

I already added -J-Dfile.encoding=UTF-8 to the /etc/netbeans.conf, added also to the VM options in configuration. Sources encoding option also set to UTF-8. No problems with the past versions of Netbeans, here I found no way to display UTF-8 characters.

我可以做什么?

推荐答案

已于 21 年 9 月 8 日更新,请注意此解决方案不适用于 NetBeans 12.x 版本.请参阅使用 Maven 项目的 NetBeans 12 UTF-8 中文输出

对于使用 Java 8 在 NetBeans 9.0 中创建的 Maven 应用程序,需要执行三个操作才能在 输出 窗口中正确呈现中文字符,前两个操作您已经在执行:

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:

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

还值得注意的是一些不必要的更改:

It's also worth noting some changes that are not necessary:

  • 无需在输出"窗口中选择特定字体(工具 > 选项 > 杂项 > 输出 > 字体),因为 等宽 的默认字体工作正常.如果不支持中文字符(例如Arial),选择另一种字体实际上可能会导致问题.
  • 无需在 {project} > 中指定 file.encoding=UTF-8属性 >运行 >虚拟机选项.
  • 无需在 pom.xml 中指定有关项目编码的任何内容.
  • 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. Selecting another font may actually cause problems if it does not support Chinese characters (e.g. Arial).
  • There's no need to specify file.encoding=UTF-8 in {project} > Properties > Run > VM Options.
  • There's no need to specify anything about the project's encoding in pom.xml.

这是代码:

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!       
}
}

这是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>

这是 NetBeans 中的输出:

This is the Output in 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
------------------------------------------------------------------------

输出,请注意:

  • 除非首先调用 System.setOut(new PrintStream(System.out, true, "UTF8"));,否则中文字符无法正确呈现.
  • 即使项目的 System.getProperty("file.encoding") 返回 "Cp1252" 而不是 "UTF,但中文字符仍会呈现-8":
  • The Chinese characters do not render correctly unless 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":

这篇关于Netbeans 9 - 打印 Unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆