简体   繁体   中英

Java System.out output in Unicode

I'm currently working on a text-based game in Java, which is played in the command line interpreter.

The problem is, that I want to echo German umlaute (line ü ö ä ß ) in the command line, which isn't supported in Windows-systems by default. However, I found a way to achieve this with a bunch of things:

  • I changed the code-page to 65001 (using chcp 65001 )
  • I changed the font to Console Lucida
  • I used a PrintStream with a UTF-8 encoding to print to the command line (in the Java-program).

After doing this, outputting UTF-8 characters on the command line works. But this isn't the default setup on every PC.

So I thought, I create a Batch file to start the game, which does the basic setup to enable the UTF-8 output. Changing the code-page and using a UTF-8 PrintStream is no problem, but changing the font is kind of tricky.

The font should be installed on every Windows since Windows XP SP2, so that shouldn't be a problem. Also, you can change the font by changing a registry entry, but this requires some privileges and is not an option.

I googled around but I didn't found any font -command. Is there a workaround to change the console-font?

I experimented a bit and you can achieve this without any scripting and without JNI: Lucida can be chosen by making a link to cmd.exe and editing the properties of that link.

I opened the link in a hex-editor to see that Lucide Console is chosen in there. The really fun thing is, that inside the link the absolute path to cmd.exe is encoded (which is D:\Win... on my system), and then i copied the link to another computer i have here to see if it magically also works there (Windows is on C-Drive on that computer) and it does: Windows fixes the absolute path to cmd.exe for you and the console that is opened on that computer is set to Lucida too.

Now all you need to open your program inside that console (and make it print in that console) all you need is a batch-file containing the name of the link-file (ending in.lnk which is hidden on most systems) followed by your program name, like so:

c.lnk t.exe

since you are using java i guess you would have to write something like:

c.lnk java sth.jar

I created a minimal example you can find here:

http://www.mediafire.com/?1yn40gsed3isjut

it contains:

  • a minimal t.exe which just pops open a MessageBox (if you dont trust t.exe just open it in notepad or pedump and see it only calls MessageBox no other api-functions)

  • the link c.lnk to cmd.exe configured to use Lucida, (the absolute path will be fixed by Windows on running it)

  • the.bat file which calls t.exe through c.lnk

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