与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字符的方法。
我能走哪条路?
发布于 2018-12-12 23:02:29
在9/8/21上进行了更新,以注意此解决方案不适用于NetBeans 12.x版本。请参阅https://stackoverflow.com/q/69101857/2985643
对于使用Java8在NetBeans 9.0中创建的Maven应用程序,需要三个操作才能使汉字在Output窗口中正确呈现,其中前两个操作已经在执行:
-J-Dfile.encoding=UTF-8
添加到属性netbeans_default_options
中,然后重新启动NetBeans。System.setOut(new PrintStream(System.out, true, "UTF8"));
,以便调用System.out.println()
时使用的打印流支持UTF-8编码。还值得注意的是,一些不必要的更改:
file.encoding=UTF-8
{}> Properties > Run > VM Options中指定。这是代码:
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中的输出:
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”,但汉字仍呈现。https://stackoverflow.com/questions/53257763
复制相似问题