Charset encoding for output to a console - not so easy, don't run

Hello,

Summary

I'd like to choose an output charset in a portable way so that the characters are displayed correctly in every environments.

Details

Under Windows, with the default encoding Java is using (I think it's UTF8), the following code System.out.println("Carr\u00e9");

does not print properly "carr?. That was expected: my Windows terminals accepts characters in CP850. On an other user's computer, the terminal will be CP1250 or I don't know what. You see the trouble.

A little try

I can do something like System.setOut(new PrintStream(System.out, true,"CP850"));

System.setErr(new PrintStream(System.err, true,"CP850"));

to switch to the proper encoding. BUT:

1) it does not change thedefault encoding (using something else than sysout or syserr will encode with UTF8)

2) it's not portable

Question(s)

Is there some portable way (even ugly such as a switch if(windows) if(linux)) to decide which encoding should be used? That would require questionning the terminal currently being used, so I was thinking about some environment variable which would be set by the terminal...

If not, what is the best strategy to make it work anyway (such as asking the user to use a switch on the command line, declaring an env. variable before launching the program, putting it into a property file, ...)? Any article / reference about that?

Once the destination platform's charset has been chosen, which call should I use to make it the default so that the Java API will use that everywhere needed?

I suppose every java developer in the world has the same problem and I'm very suprised to find no good articles on the subject!

Thank you.

Olivier

Added a "don't run" to the subject because I suspect people think I'm asking a newbie question...

Message was edited by:

OlivierMiR

[2097 byte] By [OlivierMiRa] at [2007-11-26 14:27:06]
# 1

Java is supposed to be platform neutral, and this is about as un-neutral a problem as you're going to find. It depends on which version of which operating system the app is running on, which shell it's running under, which region of the world it's in, which codepage the user has chosen, and I don't know what all. The Console class that was introduced in Java 6 is supposed to help with this kind of thing; if that doesn't work for you, or you can't use it, try looking for third-party console libraries written in Java. Scripting languages like BeanShell tend to have them, but there are probably some standalone ones out there, too. Or just write the messages to a file.

uncle_alicea at 2007-7-8 2:20:36 > top of Java-index,Desktop,Core GUI APIs...