我想打印出一个包含西里尔字母的字符串(为示例起见)。问题是在我的输出中,字符串将显示“?”字符代替。仅当我尝试在(新的或不是)Neatbeans Maven项目中的stdout上打印时,才会出现此问题。
在下面,您可以看到Maven放入了“ -Dfile.encoding = UTF-8”参数,因此我不明白为什么我的编码不是UTF-8。当我创建一个新的Neatbeans项目(没有Maven)时,编码显示为UTF-8,并且输出很好。
package com.trashmaven;
import java.nio.charset.Charset;
public class TrashMaven {
public static void main(String[] args) {
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
System.out.println("defaultCharset: " + Charset.defaultCharset());
String myString = "мусор";
System.out.println("Russian letters: " + myString);
}
}
我的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</groupId>
<artifactId>TrashMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
</project>
输出如下:
cd C:\Asztal\0Java Projekts\Testing stuff\TrashMaven; "JAVA_HOME=C:\\Program Files\\Java\\jdk-10.0.2" "M2_HOME=C:\\Program Files (x86)\\Maven\\apache-maven-3.6.0" cmd /c "\"\"C:\\Program Files (x86)\\Maven\\apache-maven-3.6.0\\bin\\mvn.cmd\" -Dexec.args=\"-classpath %classpath com.trashmaven.TrashMaven\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk-10.0.2\\bin\\java.exe\" -Dmaven.ext.class.path=\"C:\\Asztal\\0Java Projekts\\00netbeans\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 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:TrashMaven >---------------------------
Building TrashMaven 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ TrashMaven ---
file.encoding=Cp1252
defaultCharset: windows-1252
Russian letters: ?????
------------------------------------------------------------------------
BUILD SUCCESS
答案 0 :(得分:1)
设置环境变量JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
,重新启动Netbeans,然后重试。检查您要记录的值“ defaultCharset”(然后应为UTF-8)。