Issue
How do I set the output console to UTF-8 for Maven projects?
This question arose because NetBeans did not decode UTF-8 documents correctly (Nordic characters like áðíøåú etc.). By doing some search on the internet I found the solution by setting -J-Dfile.encoding=UTF-8
. Now I was able to view and edit files encoded using UTF-8.
Help -> About now showed
Product Version: Apache NetBeans IDE 12.4
Java: 16.0.1; OpenJDK 64-Bit Server VM 16.0.1+9-24
Runtime: OpenJDK Runtime Environment 16.0.1+9-24
System: Windows 10 version 10.0 running on amd64; UTF-8; en_US (nb)
-J-Dfile.encoding=UTF-8
was set by adding it to the end of netbeans_default_options
in netbeans.conf
.
This changed
System: Windows 10 version 10.0 running on amd64; Cp1252; en_US (nb)
to:
System: Windows 10 version 10.0 running on amd64; UTF-8; en_US (nb)
However the output is not showing Nordic characters properly.
If I click the settings button to the left (see image below) the Maven options dialog opens, however I don't see where I set output encoding. Maven Home is set to Bundled.
On Linux (Debian 10) the output works fine without any additional config.
Help comes from comments
As suggested I first run
System.out.println(System.getProperty("file.encoding"));
which displayed Cp1252
. Why does this happen?
And the second test using:
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
...
PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8);
System.out.println("读写汉字");
which displayed ????
. Again why does this happen when StandardCharsets
is set to UTF_8
?
I tried adding -Dfile.encoding=UTF-8
to Project Properties -> Run -> VM Options and still no change to the output.
The last thing I've tried is to remove -J-Dfile.encoding=UTF-8
from netbeans.conf
again. Checked Help -> About which again shows Cp1252
.
I created a file, inside the project src folder, encoded using UTF-8 in Notepad++ and opened it in NetBeans. To my surprise it showed the correct characters. However this surprise changed to an ahh later when I opened a UTF-8 encoded file which was outside of the project. Now the Nordic characters are not displayed correctly. And by adding -J-Dfile.encoding=UTF-8
back to the end of netbeans.conf
the Nordic characters are showed correctly again. The same goes for the Chinese characters.
Then I run the suggested tests and again to my supprise the output is UTF-8
from the file.encoding
property. However the second test still shows ????
.
I did notice one change in the output window. The first line is
cd C:\..\NetBeansProjects\..\; JAVA_HOME=E:\\...\\jdk-16.0.1 cmd /c "\"C:\\...\\netbeans\\java\\maven\\bin\\mvn.cmd\" -Dtest=ubl.InvoiceTest#outputTest -Dmaven.ext.class.path=C:\\...\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar -Dfile.encoding=UTF-8 surefire:test"
The option -Dfile.encoding=UTF-8
is close to the end of the line. And if I add -J-Dfile.encoding=UTF-8
to netbeans.conf
the first line ends with ... netbeans-eventspy.jar surefire:test". -Dfile.encoding=UTF-8 is not there. Why is that?
The Maven project
The issue I was facing is not directly due to NetBeans. Rather it is the combination of NetBeans and Maven.
- To have NetBeans show UTF-8 encoded files characters correctly the option
-J-Dfile.encoding=UTF-8
needs to be added to the end of thenetbeans_default_options
variable in thenetbeans.conf
file. - To get the output to show Nordic characters the Project settings of the Maven project needs to be set to UTF-8 (and
-J-Dfile.encoding=UTF-8
removed fromnetbeans.conf
if set).
This leaves the question of Chinese characters still open. I've tested with setting the Maven project to UTF-16, UTF-32 and Big5 without success for displaying Chinese characters. Also tried with a few font changes (Monospaced, NSimSun, SimSun, Source Serif Pro, Source Code Pro and Noto Sans) without any luck.
Thanks to
andrewjames and skomisa for their effort.
Solution
This solution applies to the initial issue with Nordic characters and leaves the question of Chinese characters still open.
- To have NetBeans show UTF-8 encoded files characters correctly the option
-J-Dfile.encoding=UTF-8
needs to be added to the end of thenetbeans_default_options
variable in thenetbeans.conf
file which is located in the NetBeans installation folder under/etc
. - To get the output to show Nordic characters correctly the Project settings of the Maven project needs to be set to
UTF-8
(and-J-Dfile.encoding=UTF-8
removed fromnetbeans.conf
if set). This can be done by right clicking the project and selecting Properties. Click the Sources category. There you can choose which Encoding to use. This setting applies to the current project.
Answered By - jacobm
Answer Checked By - Mary Flores (JavaFixing Volunteer)