1

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
9
  • Could you please show exec-maven-plugin configuration in pom.xml?
    – amseager
    Commented Feb 26, 2019 at 20:48
  • Also it seems that the problem is not with NetBeans but with default encoding of Windows terminal. Try to launch the same from cmd.exe. And then execute "chcp 1251" in cmd and launch again.
    – amseager
    Commented Feb 26, 2019 at 20:52
  • I'm not sure how can I show the "exec-maven-plugin" configuration, I linked the whole pom.xml. I cannot launch the same command from cmd.exe, it gives "The system cannot find the path specified" error. If it was the default encoding of Windows terminal, shouldn't I get the same result when I run non-Maven simple projects?
    – Trefort
    Commented Feb 26, 2019 at 21:18
  • Maven uses OS terminal (in your case cmd) to show output, which is simply displaying as another tab "Terminal" (it's called the "external" or "integrated" terminal, can be opened manually by Tools -> Open in terminal), while Netbeans itself runs apps in its own "Output" tab (also called "internal terminal"). The same is with IntelliJ IDEA btw. With "launch the same from cmd" I meant simply to compile and run your app from cmd in order to check potential encoding problem (sorry for possible misunderstanding).
    – amseager
    Commented Feb 26, 2019 at 21:58
  • 1
    I would recommend you 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).
    – amseager
    Commented Feb 27, 2019 at 9:46

1 Answer 1

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.