3

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.

Output window

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 the netbeans_default_options variable in the netbeans.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 from netbeans.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.

jacobm
  • 81
  • 5
  • 1
    To confirm the encoding being used, run this: `System.out.println(System.getProperty("file.encoding"));`. Also, a test, try this: `import java.io.PrintStream;` and `import java.nio.charset.StandardCharsets` - and then `PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8);` followed by `out.println("读写汉字");`. What happens in each case? You can [edit] your question. – andrewJames Sep 08 '21 at 12:55
  • (1) How _exactly_ did you add `-J-Dfile.encoding=UTF-8` to your conf file? (2) Another test: add `-Dfile.encoding=UTF-8` to your project properties (typically by going to File > Project Properties > Run > VM Options). (3) What "output" font are you using? Check in Tools > Options > Miscellaneous > Output > Font. (4) Please **EDIT** the question, instead of adding comments for these things - your notes belong in the question. – andrewJames Sep 08 '21 at 13:56
  • Don't add `-J-Dfile.encoding=UTF-8` to VM properties. Add `-Dfile.encoding=UTF-8`. – andrewJames Sep 08 '21 at 15:52
  • A couple of points: [1] This is happening because you have a Maven project. Chinese characters can be written to the Output window without taking any special action at all with Ant projects. [2] The accepted answer to [Netbeans 9 - Print Unicode Characters](https://stackoverflow.com/q/53257763/2985643) solves your exact problem on NetBeans 9, but unfortunately that solution does not work with NetBeans 16. – skomisa Sep 08 '21 at 18:30
  • Also, I made some minor edits to the OP to reflect that this issue specifically relates to Maven projects writing Chinese characters to the Output window on NetBeans 12.4. – skomisa Sep 08 '21 at 18:32
  • @skomisa - sorry if I confused things. The Chinese characters were my characters. The question does not show the actual characters which failed to be displayed. Those are not Chinese - probably just accented Latin. My Chinese was more for testing: "if it works with these, then it should work with yours". – andrewJames Sep 08 '21 at 18:42
  • @andrewjames OK, I did not realize that, but no problem. My primary aim was to clarify that the issue was specific to Maven projects. I tested with Chinese text on NB 16, so the post is still accurate, but it would be helpful if the OP updated the question to provide some sample text. I also tested with some simple German text and even that did not render correctly in the Output window. – skomisa Sep 08 '21 at 19:07
  • 1
    I suspect this issue is related to the version of `exec-maven-plugin` being used, rather than NetBeans itself. See open bug [useMavenLogger breaks nōn-ASCII characters (i.e. 99.9̅% of Unicode)](https://github.com/mojohaus/exec-maven-plugin/issues/158). Rendering non ASCII characters in the Output window for Maven projects worked fine with `exec-maven-plugin:1.5.0` in NetBeans 9, but NetBeans 16 is using `exec-maven-plugin:3.0.0`. – skomisa Sep 08 '21 at 20:05
  • Suggestion: If this is not a duplicate question, you can [write your own answer](https://stackoverflow.com/help/self-answer), to help future visitors to this question. (Final thought: Bear in mind that not all fonts contain every possible character/symbol/glyph - so, even if that is not the problem in this specific Maven-related case, it's worth checking which font you are using to display your output.) – andrewJames Sep 09 '21 at 12:31

1 Answers1

1

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 the netbeans_default_options variable in the netbeans.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 from netbeans.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.
jacobm
  • 81
  • 5