简体   繁体   中英

java print unicode characters to bash shell (mac OsX)

I have this code in java 1.6:

System.out.println("\u00b2");

but on bash on OSX10.6 I get question marks and not the unicode characters...

actually I want to print the characters 176,177,178 on the extended ascii code (look here http://www.asciitable.com/ ) to create some art on the bash terminal..

any idea?

thanks

The following code works for me in UTF-8 enabled Terminal.app on Mac OS X 10.6.7:

# code taken from: 
# "Print Unicode characters to the Terminal with Java",
# http://hints.macworld.com/article.php?story=20050208053951714

echo '
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
class Test {
  public static void main (String[] argv) throws UnsupportedEncodingException {
  String unicodeMessage = "\u00b2\u2591\u2592\u2593";
  PrintStream out = new PrintStream(System.out, true, "UTF-8");
  out.println(unicodeMessage);
  }
}
' > test.java

javac test.java
java Test

Is bash in a UTF8 locale? Type in the locale command and see if your LANG looks appropriate (eg something like *en_GB.UTF-8*). If not, update the value of LANG and try again.

First of all, you need to be certain that the character encoding in your terminal session matches what your java application is outputting. You most likely want UTF-8 which I believe is standard under OS X.

Next you need to be certain that the font used in your terminal session actually contains the characters you want to see. These seem to be rare and may not be available in all fonts.

验证是否已选中Terminal > Preferences > Encodings UTF-8。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM