Netbeans 9 - Print Unicode Characters
up vote
0
down vote
favorite
With Netbeans 9:
Product Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)
Java: 1.8.0_181; Java HotSpot(TM) 64-Bit Server VM 25.181-b13
Runtime: Java(TM) SE Runtime Environment 1.8.0_181-b13
System: Windows 10 version 10.0 running on amd64; UTF-8; en_EN (nb)
I want to be able to print:
String text = "你好!";
System.out.println(text);
The result is instead:
--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaApplication1 ---
???
I already added -J-Dfile.encoding=UTF-8 to the /etc/netbeans.conf, added also to the VM options in configuration. Sources encoding option also set to UTF-8.
No problems with the past versions of Netbeans, here I found no way to display UTF-8 characters.
Which way can I do?
java netbeans utf-8 system.out netbeans-9
add a comment |
up vote
0
down vote
favorite
With Netbeans 9:
Product Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)
Java: 1.8.0_181; Java HotSpot(TM) 64-Bit Server VM 25.181-b13
Runtime: Java(TM) SE Runtime Environment 1.8.0_181-b13
System: Windows 10 version 10.0 running on amd64; UTF-8; en_EN (nb)
I want to be able to print:
String text = "你好!";
System.out.println(text);
The result is instead:
--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaApplication1 ---
???
I already added -J-Dfile.encoding=UTF-8 to the /etc/netbeans.conf, added also to the VM options in configuration. Sources encoding option also set to UTF-8.
No problems with the past versions of Netbeans, here I found no way to display UTF-8 characters.
Which way can I do?
java netbeans utf-8 system.out netbeans-9
You need to configure a font for the console that is capable of displaying those characters. Right click in the output window, then choose "Settings" and try a different font.
– a_horse_with_no_name
Nov 12 at 7:54
Not in this case. Chinese characters can be displayed by the monospace, I did that many times. Furthermore if they couldn't I would see a little square, not a ?. I tried to replace a proper font as your suggestion, just to be 100% sure.
– donnadulcinea
Nov 12 at 8:44
Maybe it's a Maven problem, with an Ant based project this works fine for me.
– a_horse_with_no_name
Nov 12 at 8:49
Thank you that was a great hint, I didn't try that. Unfortunately I found out my pom.xml was already OK as suggested here for example: stackoverflow.com/a/10375505/3200736 I explicitely defined the plugins and stil doesn't work. Weird.
– donnadulcinea
Nov 12 at 9:14
1
This really looks like an NB 9 issue. Running a Maven project that writes Chinese characters to the output window works fine with NB 8.2, but renders the characters incorrectly when running the same project with NB 9. I have set the UTF-8 configurations in NB 8.2 and NB 9. I have also configured both NB 8 and NB 9 to use Maven 3.5.0 (Tools > Options > Java > Maven Home) and JDK 1.8. So: same project, same Maven, same JDK, same configuration. The only difference is the version of NetBeans. (No problem rendering Chinese characters for a non-Maven project in NB 9.)
– skomisa
Nov 13 at 7:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
With Netbeans 9:
Product Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)
Java: 1.8.0_181; Java HotSpot(TM) 64-Bit Server VM 25.181-b13
Runtime: Java(TM) SE Runtime Environment 1.8.0_181-b13
System: Windows 10 version 10.0 running on amd64; UTF-8; en_EN (nb)
I want to be able to print:
String text = "你好!";
System.out.println(text);
The result is instead:
--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaApplication1 ---
???
I already added -J-Dfile.encoding=UTF-8 to the /etc/netbeans.conf, added also to the VM options in configuration. Sources encoding option also set to UTF-8.
No problems with the past versions of Netbeans, here I found no way to display UTF-8 characters.
Which way can I do?
java netbeans utf-8 system.out netbeans-9
With Netbeans 9:
Product Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)
Java: 1.8.0_181; Java HotSpot(TM) 64-Bit Server VM 25.181-b13
Runtime: Java(TM) SE Runtime Environment 1.8.0_181-b13
System: Windows 10 version 10.0 running on amd64; UTF-8; en_EN (nb)
I want to be able to print:
String text = "你好!";
System.out.println(text);
The result is instead:
--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaApplication1 ---
???
I already added -J-Dfile.encoding=UTF-8 to the /etc/netbeans.conf, added also to the VM options in configuration. Sources encoding option also set to UTF-8.
No problems with the past versions of Netbeans, here I found no way to display UTF-8 characters.
Which way can I do?
java netbeans utf-8 system.out netbeans-9
java netbeans utf-8 system.out netbeans-9
edited Nov 13 at 17:51
Halvor Strand
14.6k145372
14.6k145372
asked Nov 12 at 7:45
donnadulcinea
99611223
99611223
You need to configure a font for the console that is capable of displaying those characters. Right click in the output window, then choose "Settings" and try a different font.
– a_horse_with_no_name
Nov 12 at 7:54
Not in this case. Chinese characters can be displayed by the monospace, I did that many times. Furthermore if they couldn't I would see a little square, not a ?. I tried to replace a proper font as your suggestion, just to be 100% sure.
– donnadulcinea
Nov 12 at 8:44
Maybe it's a Maven problem, with an Ant based project this works fine for me.
– a_horse_with_no_name
Nov 12 at 8:49
Thank you that was a great hint, I didn't try that. Unfortunately I found out my pom.xml was already OK as suggested here for example: stackoverflow.com/a/10375505/3200736 I explicitely defined the plugins and stil doesn't work. Weird.
– donnadulcinea
Nov 12 at 9:14
1
This really looks like an NB 9 issue. Running a Maven project that writes Chinese characters to the output window works fine with NB 8.2, but renders the characters incorrectly when running the same project with NB 9. I have set the UTF-8 configurations in NB 8.2 and NB 9. I have also configured both NB 8 and NB 9 to use Maven 3.5.0 (Tools > Options > Java > Maven Home) and JDK 1.8. So: same project, same Maven, same JDK, same configuration. The only difference is the version of NetBeans. (No problem rendering Chinese characters for a non-Maven project in NB 9.)
– skomisa
Nov 13 at 7:13
add a comment |
You need to configure a font for the console that is capable of displaying those characters. Right click in the output window, then choose "Settings" and try a different font.
– a_horse_with_no_name
Nov 12 at 7:54
Not in this case. Chinese characters can be displayed by the monospace, I did that many times. Furthermore if they couldn't I would see a little square, not a ?. I tried to replace a proper font as your suggestion, just to be 100% sure.
– donnadulcinea
Nov 12 at 8:44
Maybe it's a Maven problem, with an Ant based project this works fine for me.
– a_horse_with_no_name
Nov 12 at 8:49
Thank you that was a great hint, I didn't try that. Unfortunately I found out my pom.xml was already OK as suggested here for example: stackoverflow.com/a/10375505/3200736 I explicitely defined the plugins and stil doesn't work. Weird.
– donnadulcinea
Nov 12 at 9:14
1
This really looks like an NB 9 issue. Running a Maven project that writes Chinese characters to the output window works fine with NB 8.2, but renders the characters incorrectly when running the same project with NB 9. I have set the UTF-8 configurations in NB 8.2 and NB 9. I have also configured both NB 8 and NB 9 to use Maven 3.5.0 (Tools > Options > Java > Maven Home) and JDK 1.8. So: same project, same Maven, same JDK, same configuration. The only difference is the version of NetBeans. (No problem rendering Chinese characters for a non-Maven project in NB 9.)
– skomisa
Nov 13 at 7:13
You need to configure a font for the console that is capable of displaying those characters. Right click in the output window, then choose "Settings" and try a different font.
– a_horse_with_no_name
Nov 12 at 7:54
You need to configure a font for the console that is capable of displaying those characters. Right click in the output window, then choose "Settings" and try a different font.
– a_horse_with_no_name
Nov 12 at 7:54
Not in this case. Chinese characters can be displayed by the monospace, I did that many times. Furthermore if they couldn't I would see a little square, not a ?. I tried to replace a proper font as your suggestion, just to be 100% sure.
– donnadulcinea
Nov 12 at 8:44
Not in this case. Chinese characters can be displayed by the monospace, I did that many times. Furthermore if they couldn't I would see a little square, not a ?. I tried to replace a proper font as your suggestion, just to be 100% sure.
– donnadulcinea
Nov 12 at 8:44
Maybe it's a Maven problem, with an Ant based project this works fine for me.
– a_horse_with_no_name
Nov 12 at 8:49
Maybe it's a Maven problem, with an Ant based project this works fine for me.
– a_horse_with_no_name
Nov 12 at 8:49
Thank you that was a great hint, I didn't try that. Unfortunately I found out my pom.xml was already OK as suggested here for example: stackoverflow.com/a/10375505/3200736 I explicitely defined the plugins and stil doesn't work. Weird.
– donnadulcinea
Nov 12 at 9:14
Thank you that was a great hint, I didn't try that. Unfortunately I found out my pom.xml was already OK as suggested here for example: stackoverflow.com/a/10375505/3200736 I explicitely defined the plugins and stil doesn't work. Weird.
– donnadulcinea
Nov 12 at 9:14
1
1
This really looks like an NB 9 issue. Running a Maven project that writes Chinese characters to the output window works fine with NB 8.2, but renders the characters incorrectly when running the same project with NB 9. I have set the UTF-8 configurations in NB 8.2 and NB 9. I have also configured both NB 8 and NB 9 to use Maven 3.5.0 (Tools > Options > Java > Maven Home) and JDK 1.8. So: same project, same Maven, same JDK, same configuration. The only difference is the version of NetBeans. (No problem rendering Chinese characters for a non-Maven project in NB 9.)
– skomisa
Nov 13 at 7:13
This really looks like an NB 9 issue. Running a Maven project that writes Chinese characters to the output window works fine with NB 8.2, but renders the characters incorrectly when running the same project with NB 9. I have set the UTF-8 configurations in NB 8.2 and NB 9. I have also configured both NB 8 and NB 9 to use Maven 3.5.0 (Tools > Options > Java > Maven Home) and JDK 1.8. So: same project, same Maven, same JDK, same configuration. The only difference is the version of NetBeans. (No problem rendering Chinese characters for a non-Maven project in NB 9.)
– skomisa
Nov 13 at 7:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
For a Maven application created in NetBeans 9.0 using Java 8 there are three actions needed to get Chinese characters to render correctly in the Output window, the first two of which you were already doing:
- Add
-J-Dfile.encoding=UTF-8to the propertynetbeans_default_optionsin file etc/netbeans.conf, and then restart NetBeans. - From the Projects panel set {project} > Properties > Source > Encoding to UTF-8.
- In the application call
System.setOut(new PrintStream(System.out, true, "UTF8"));so that the print stream used when callingSystem.out.println()supports UTF-8 encoding.
It's also worth noting some changes that are not necessary:
- There's no need to select a specific font in the Output window (Tools > Options > Miscellaneous > Output > Font) since the default font of Monospaced works fine. Selecting another font may actually cause problems if it does not support Chinese characters (e.g. Arial).
- There's no need to specify
file.encoding=UTF-8in {project} > Properties > Run > VM Options. - There's no need to specify anything about the project's encoding in pom.xml.
This is the code:
package com.unthreading.mavenchinesechars;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class ChineseChars {
public static void main(String args) throws UnsupportedEncodingException {
System.out.println("System.getProperty("file.encoding"): " + System.getProperty("file.encoding"));
System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset());
System.out.println("System.getProperty("java.version"): " + System.getProperty("java.version"));
String text = "你好!";
System.out.println(text); // <<<======================= Fails!
System.setOut(new PrintStream(System.out, true, "UTF8")); // Essential!
System.out.println(text); // <<<======================= Works!
}
}
This is 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.unthreading</groupId>
<artifactId>MavenChineseChars</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This is the Output in NetBeans:
cd D:NB82MavenChineseChars; JAVA_HOME=C:\Java\jdk1.8.0_181 M2_HOME=C:\apache-maven-3.6.0 cmd /c """C:\apache-maven-3.6.0\bin\mvn.cmd" -Dexec.args="-classpath %classpath com.unthreading.mavenchinesechars.ChineseChars" -Dexec.executable=C:\Java\jdk1.8.0_181\bin\java.exe -Dmaven.ext.class.path=C:\NetBeans9\java\maven-nblib\netbeans-eventspy.jar 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.unthreading:MavenChineseChars >------------------
Building MavenChineseChars 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ MavenChineseChars ---
System.getProperty("file.encoding"): Cp1252
Charset.defaultCharset(): windows-1252
System.getProperty("java.version"): 1.8.0_181
???
你好!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.021 s
Finished at: 2018-12-12T18:24:12-05:00
------------------------------------------------------------------------
From the Output, note that:
- The Chinese characters do not render correctly unless
System.setOut(new PrintStream(System.out, true, "UTF8"));is called first. - The Chinese characters render even though
System.getProperty("file.encoding")for the project returns "Cp1252" rather than "UTF-8":
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!
– donnadulcinea
Dec 13 at 5:50
@donnadulcinea OK. I have never used it, but there is a methodSystem.setIn(InputStream)which presumably works in a similar way toSystem.setOut(PrintStream). So first create yourInputStreamto support UTF-8, then pass thatInputStreamtoSystem.setIn(InputStream)before declaring yourScanner.
– skomisa
Dec 13 at 6:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257763%2fnetbeans-9-print-unicode-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
For a Maven application created in NetBeans 9.0 using Java 8 there are three actions needed to get Chinese characters to render correctly in the Output window, the first two of which you were already doing:
- Add
-J-Dfile.encoding=UTF-8to the propertynetbeans_default_optionsin file etc/netbeans.conf, and then restart NetBeans. - From the Projects panel set {project} > Properties > Source > Encoding to UTF-8.
- In the application call
System.setOut(new PrintStream(System.out, true, "UTF8"));so that the print stream used when callingSystem.out.println()supports UTF-8 encoding.
It's also worth noting some changes that are not necessary:
- There's no need to select a specific font in the Output window (Tools > Options > Miscellaneous > Output > Font) since the default font of Monospaced works fine. Selecting another font may actually cause problems if it does not support Chinese characters (e.g. Arial).
- There's no need to specify
file.encoding=UTF-8in {project} > Properties > Run > VM Options. - There's no need to specify anything about the project's encoding in pom.xml.
This is the code:
package com.unthreading.mavenchinesechars;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class ChineseChars {
public static void main(String args) throws UnsupportedEncodingException {
System.out.println("System.getProperty("file.encoding"): " + System.getProperty("file.encoding"));
System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset());
System.out.println("System.getProperty("java.version"): " + System.getProperty("java.version"));
String text = "你好!";
System.out.println(text); // <<<======================= Fails!
System.setOut(new PrintStream(System.out, true, "UTF8")); // Essential!
System.out.println(text); // <<<======================= Works!
}
}
This is 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.unthreading</groupId>
<artifactId>MavenChineseChars</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This is the Output in NetBeans:
cd D:NB82MavenChineseChars; JAVA_HOME=C:\Java\jdk1.8.0_181 M2_HOME=C:\apache-maven-3.6.0 cmd /c """C:\apache-maven-3.6.0\bin\mvn.cmd" -Dexec.args="-classpath %classpath com.unthreading.mavenchinesechars.ChineseChars" -Dexec.executable=C:\Java\jdk1.8.0_181\bin\java.exe -Dmaven.ext.class.path=C:\NetBeans9\java\maven-nblib\netbeans-eventspy.jar 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.unthreading:MavenChineseChars >------------------
Building MavenChineseChars 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ MavenChineseChars ---
System.getProperty("file.encoding"): Cp1252
Charset.defaultCharset(): windows-1252
System.getProperty("java.version"): 1.8.0_181
???
你好!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.021 s
Finished at: 2018-12-12T18:24:12-05:00
------------------------------------------------------------------------
From the Output, note that:
- The Chinese characters do not render correctly unless
System.setOut(new PrintStream(System.out, true, "UTF8"));is called first. - The Chinese characters render even though
System.getProperty("file.encoding")for the project returns "Cp1252" rather than "UTF-8":
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!
– donnadulcinea
Dec 13 at 5:50
@donnadulcinea OK. I have never used it, but there is a methodSystem.setIn(InputStream)which presumably works in a similar way toSystem.setOut(PrintStream). So first create yourInputStreamto support UTF-8, then pass thatInputStreamtoSystem.setIn(InputStream)before declaring yourScanner.
– skomisa
Dec 13 at 6:35
add a comment |
up vote
1
down vote
accepted
For a Maven application created in NetBeans 9.0 using Java 8 there are three actions needed to get Chinese characters to render correctly in the Output window, the first two of which you were already doing:
- Add
-J-Dfile.encoding=UTF-8to the propertynetbeans_default_optionsin file etc/netbeans.conf, and then restart NetBeans. - From the Projects panel set {project} > Properties > Source > Encoding to UTF-8.
- In the application call
System.setOut(new PrintStream(System.out, true, "UTF8"));so that the print stream used when callingSystem.out.println()supports UTF-8 encoding.
It's also worth noting some changes that are not necessary:
- There's no need to select a specific font in the Output window (Tools > Options > Miscellaneous > Output > Font) since the default font of Monospaced works fine. Selecting another font may actually cause problems if it does not support Chinese characters (e.g. Arial).
- There's no need to specify
file.encoding=UTF-8in {project} > Properties > Run > VM Options. - There's no need to specify anything about the project's encoding in pom.xml.
This is the code:
package com.unthreading.mavenchinesechars;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class ChineseChars {
public static void main(String args) throws UnsupportedEncodingException {
System.out.println("System.getProperty("file.encoding"): " + System.getProperty("file.encoding"));
System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset());
System.out.println("System.getProperty("java.version"): " + System.getProperty("java.version"));
String text = "你好!";
System.out.println(text); // <<<======================= Fails!
System.setOut(new PrintStream(System.out, true, "UTF8")); // Essential!
System.out.println(text); // <<<======================= Works!
}
}
This is 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.unthreading</groupId>
<artifactId>MavenChineseChars</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This is the Output in NetBeans:
cd D:NB82MavenChineseChars; JAVA_HOME=C:\Java\jdk1.8.0_181 M2_HOME=C:\apache-maven-3.6.0 cmd /c """C:\apache-maven-3.6.0\bin\mvn.cmd" -Dexec.args="-classpath %classpath com.unthreading.mavenchinesechars.ChineseChars" -Dexec.executable=C:\Java\jdk1.8.0_181\bin\java.exe -Dmaven.ext.class.path=C:\NetBeans9\java\maven-nblib\netbeans-eventspy.jar 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.unthreading:MavenChineseChars >------------------
Building MavenChineseChars 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ MavenChineseChars ---
System.getProperty("file.encoding"): Cp1252
Charset.defaultCharset(): windows-1252
System.getProperty("java.version"): 1.8.0_181
???
你好!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.021 s
Finished at: 2018-12-12T18:24:12-05:00
------------------------------------------------------------------------
From the Output, note that:
- The Chinese characters do not render correctly unless
System.setOut(new PrintStream(System.out, true, "UTF8"));is called first. - The Chinese characters render even though
System.getProperty("file.encoding")for the project returns "Cp1252" rather than "UTF-8":
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!
– donnadulcinea
Dec 13 at 5:50
@donnadulcinea OK. I have never used it, but there is a methodSystem.setIn(InputStream)which presumably works in a similar way toSystem.setOut(PrintStream). So first create yourInputStreamto support UTF-8, then pass thatInputStreamtoSystem.setIn(InputStream)before declaring yourScanner.
– skomisa
Dec 13 at 6:35
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
For a Maven application created in NetBeans 9.0 using Java 8 there are three actions needed to get Chinese characters to render correctly in the Output window, the first two of which you were already doing:
- Add
-J-Dfile.encoding=UTF-8to the propertynetbeans_default_optionsin file etc/netbeans.conf, and then restart NetBeans. - From the Projects panel set {project} > Properties > Source > Encoding to UTF-8.
- In the application call
System.setOut(new PrintStream(System.out, true, "UTF8"));so that the print stream used when callingSystem.out.println()supports UTF-8 encoding.
It's also worth noting some changes that are not necessary:
- There's no need to select a specific font in the Output window (Tools > Options > Miscellaneous > Output > Font) since the default font of Monospaced works fine. Selecting another font may actually cause problems if it does not support Chinese characters (e.g. Arial).
- There's no need to specify
file.encoding=UTF-8in {project} > Properties > Run > VM Options. - There's no need to specify anything about the project's encoding in pom.xml.
This is the code:
package com.unthreading.mavenchinesechars;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class ChineseChars {
public static void main(String args) throws UnsupportedEncodingException {
System.out.println("System.getProperty("file.encoding"): " + System.getProperty("file.encoding"));
System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset());
System.out.println("System.getProperty("java.version"): " + System.getProperty("java.version"));
String text = "你好!";
System.out.println(text); // <<<======================= Fails!
System.setOut(new PrintStream(System.out, true, "UTF8")); // Essential!
System.out.println(text); // <<<======================= Works!
}
}
This is 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.unthreading</groupId>
<artifactId>MavenChineseChars</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This is the Output in NetBeans:
cd D:NB82MavenChineseChars; JAVA_HOME=C:\Java\jdk1.8.0_181 M2_HOME=C:\apache-maven-3.6.0 cmd /c """C:\apache-maven-3.6.0\bin\mvn.cmd" -Dexec.args="-classpath %classpath com.unthreading.mavenchinesechars.ChineseChars" -Dexec.executable=C:\Java\jdk1.8.0_181\bin\java.exe -Dmaven.ext.class.path=C:\NetBeans9\java\maven-nblib\netbeans-eventspy.jar 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.unthreading:MavenChineseChars >------------------
Building MavenChineseChars 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ MavenChineseChars ---
System.getProperty("file.encoding"): Cp1252
Charset.defaultCharset(): windows-1252
System.getProperty("java.version"): 1.8.0_181
???
你好!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.021 s
Finished at: 2018-12-12T18:24:12-05:00
------------------------------------------------------------------------
From the Output, note that:
- The Chinese characters do not render correctly unless
System.setOut(new PrintStream(System.out, true, "UTF8"));is called first. - The Chinese characters render even though
System.getProperty("file.encoding")for the project returns "Cp1252" rather than "UTF-8":
For a Maven application created in NetBeans 9.0 using Java 8 there are three actions needed to get Chinese characters to render correctly in the Output window, the first two of which you were already doing:
- Add
-J-Dfile.encoding=UTF-8to the propertynetbeans_default_optionsin file etc/netbeans.conf, and then restart NetBeans. - From the Projects panel set {project} > Properties > Source > Encoding to UTF-8.
- In the application call
System.setOut(new PrintStream(System.out, true, "UTF8"));so that the print stream used when callingSystem.out.println()supports UTF-8 encoding.
It's also worth noting some changes that are not necessary:
- There's no need to select a specific font in the Output window (Tools > Options > Miscellaneous > Output > Font) since the default font of Monospaced works fine. Selecting another font may actually cause problems if it does not support Chinese characters (e.g. Arial).
- There's no need to specify
file.encoding=UTF-8in {project} > Properties > Run > VM Options. - There's no need to specify anything about the project's encoding in pom.xml.
This is the code:
package com.unthreading.mavenchinesechars;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class ChineseChars {
public static void main(String args) throws UnsupportedEncodingException {
System.out.println("System.getProperty("file.encoding"): " + System.getProperty("file.encoding"));
System.out.println("Charset.defaultCharset(): " + Charset.defaultCharset());
System.out.println("System.getProperty("java.version"): " + System.getProperty("java.version"));
String text = "你好!";
System.out.println(text); // <<<======================= Fails!
System.setOut(new PrintStream(System.out, true, "UTF8")); // Essential!
System.out.println(text); // <<<======================= Works!
}
}
This is 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.unthreading</groupId>
<artifactId>MavenChineseChars</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This is the Output in NetBeans:
cd D:NB82MavenChineseChars; JAVA_HOME=C:\Java\jdk1.8.0_181 M2_HOME=C:\apache-maven-3.6.0 cmd /c """C:\apache-maven-3.6.0\bin\mvn.cmd" -Dexec.args="-classpath %classpath com.unthreading.mavenchinesechars.ChineseChars" -Dexec.executable=C:\Java\jdk1.8.0_181\bin\java.exe -Dmaven.ext.class.path=C:\NetBeans9\java\maven-nblib\netbeans-eventspy.jar 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.unthreading:MavenChineseChars >------------------
Building MavenChineseChars 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ MavenChineseChars ---
System.getProperty("file.encoding"): Cp1252
Charset.defaultCharset(): windows-1252
System.getProperty("java.version"): 1.8.0_181
???
你好!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.021 s
Finished at: 2018-12-12T18:24:12-05:00
------------------------------------------------------------------------
From the Output, note that:
- The Chinese characters do not render correctly unless
System.setOut(new PrintStream(System.out, true, "UTF8"));is called first. - The Chinese characters render even though
System.getProperty("file.encoding")for the project returns "Cp1252" rather than "UTF-8":
edited Dec 12 at 23:31
answered Dec 12 at 23:02
skomisa
5,43621632
5,43621632
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!
– donnadulcinea
Dec 13 at 5:50
@donnadulcinea OK. I have never used it, but there is a methodSystem.setIn(InputStream)which presumably works in a similar way toSystem.setOut(PrintStream). So first create yourInputStreamto support UTF-8, then pass thatInputStreamtoSystem.setIn(InputStream)before declaring yourScanner.
– skomisa
Dec 13 at 6:35
add a comment |
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!
– donnadulcinea
Dec 13 at 5:50
@donnadulcinea OK. I have never used it, but there is a methodSystem.setIn(InputStream)which presumably works in a similar way toSystem.setOut(PrintStream). So first create yourInputStreamto support UTF-8, then pass thatInputStreamtoSystem.setIn(InputStream)before declaring yourScanner.
– skomisa
Dec 13 at 6:35
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (
Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!– donnadulcinea
Dec 13 at 5:50
Thank you for your answer. Actually I was also setting up the correct parameters on the PrintStream. Your answer anyway brought me to the real issue which is the Scanner. Even if well setup (
Scanner scanner = new Scanner(System.in, "UTF-8");) is losing UTF8 format for some reason, I'll check why. Thank you!– donnadulcinea
Dec 13 at 5:50
@donnadulcinea OK. I have never used it, but there is a method
System.setIn(InputStream) which presumably works in a similar way to System.setOut(PrintStream). So first create your InputStream to support UTF-8, then pass that InputStream to System.setIn(InputStream) before declaring your Scanner.– skomisa
Dec 13 at 6:35
@donnadulcinea OK. I have never used it, but there is a method
System.setIn(InputStream) which presumably works in a similar way to System.setOut(PrintStream). So first create your InputStream to support UTF-8, then pass that InputStream to System.setIn(InputStream) before declaring your Scanner.– skomisa
Dec 13 at 6:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257763%2fnetbeans-9-print-unicode-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to configure a font for the console that is capable of displaying those characters. Right click in the output window, then choose "Settings" and try a different font.
– a_horse_with_no_name
Nov 12 at 7:54
Not in this case. Chinese characters can be displayed by the monospace, I did that many times. Furthermore if they couldn't I would see a little square, not a ?. I tried to replace a proper font as your suggestion, just to be 100% sure.
– donnadulcinea
Nov 12 at 8:44
Maybe it's a Maven problem, with an Ant based project this works fine for me.
– a_horse_with_no_name
Nov 12 at 8:49
Thank you that was a great hint, I didn't try that. Unfortunately I found out my pom.xml was already OK as suggested here for example: stackoverflow.com/a/10375505/3200736 I explicitely defined the plugins and stil doesn't work. Weird.
– donnadulcinea
Nov 12 at 9:14
1
This really looks like an NB 9 issue. Running a Maven project that writes Chinese characters to the output window works fine with NB 8.2, but renders the characters incorrectly when running the same project with NB 9. I have set the UTF-8 configurations in NB 8.2 and NB 9. I have also configured both NB 8 and NB 9 to use Maven 3.5.0 (Tools > Options > Java > Maven Home) and JDK 1.8. So: same project, same Maven, same JDK, same configuration. The only difference is the version of NetBeans. (No problem rendering Chinese characters for a non-Maven project in NB 9.)
– skomisa
Nov 13 at 7:13