Issue
I want to print out a String that contains Cyrillic letters (for the sake of example). The problem is that on my output the string will show '?' characters instead. This problem only occurs if I try to print on stdout in a (fresh or not) Neatbeans Maven project.
Below you can see that Maven puts the "-Dfile.encoding=UTF-8" parameter in, so I dont understand why is my encoding not UTF-8. When I create a fresh Neatbeans project (without Maven) the encoding shows UTF-8 and the output is fine.
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);
}
}
My 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>
The output is the following:
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
Solution
Set the environment variable JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
, restart Netbeans and try again. Check the value "defaultCharset" that you're logging (It should be UTF-8 then).
Answered By - amseager
Answer Checked By - Pedro (JavaFixing Volunteer)