0

I've made some progress on my previous question, and discovered that the message being displayed is redirected from stderr and is encoded with Cp850.

So, how can I discover this from the program in Java? Reading the Wikipedia page on code page 850, and looking at the characters that can be encoded, it's obvious that this won't be used on all Windows systems because it doesn't include the characters needed for, say, Japanese. (And I assume that it won't be used on non-windows systems.)

If I print out the results of Charset.defaultCharset(), I get windows-1252. But using Cp1252 to decode the output from stderr doesn't give the correct encoding -- what should be è prints as Š.

This has been asked before in a slightly different way, but the answers given are just wrong or don't apply to what I'm trying to do. Is this possible to do in Java? If not, what are people doing to get a stderr message to the user in a format they can read?

Community
  • 1
  • 1
KathyA.
  • 671
  • 6
  • 14
  • 1
    Unfortunately, there is no way to know the tty capabilities programatically in Java unless you resort to native APIs – fge Mar 14 '14 at 21:55
  • Well, there _may_ be one solution; do you use a startup script for your service? If yes, maybe you can use OS commands to find the correct encoding and passing a `-D` option to the JVM at startup – fge Mar 14 '14 at 21:59

1 Answers1

0

Here's what worked for me, for the benefit of future Stack Overflowers.

import com.sun.jna.Library;
import com.sun.jna.Native;

public static int getCodePage(){
  Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
  return lib.GetOEMCP();
}

public interface Kernel32 extends Library {
  public int GetOEMCP();
}

The code page returned will be an int corresponding to the list at Code Page Identifiers. These can be correlated to the list of supported encodings for Java. (I made a static hashmap. I'm sure there are other, nicer ways to do it.)

KathyA.
  • 671
  • 6
  • 14