3

I'm running a Java program on Mac OS X 10.8 (from the Terminal), and the Java VM's file.encoding is defaulting to "SJIS". I even tried a tiny Java program that does nothing but print out the system properties, and that shows that the Java VM itself is defaulting to SJIS. (And let's face it, nobody likes SJIS!)

My terminal is set to UTF-8. My $LANG isn't set, and $LC_CTYPE is "UTF-8". No environmental variable or Terminal setting has "JIS" in it anywhere, AFAICT.

On Java on Mac OS X, where does it get the file.encoding value from? Is there a system setting I can change so that it uses UTF-8? I don't want to have to specify this per-application and per-library, because that is somewhere between "awkward" and "impossible", depending on the program.

I suppose I'm OK with setting an environmental variable for this, but I can't find any standard variables (like $LANG) that affect Java.

1

You could always put this code into a java class in your favorite IDE

import java.nio.charset.Charset;
(...)
Charset.defaultCharset()

Then follow defaultCharset() link to understand how your JVM instance will get determine default charset. Example on win 64b hotspot JVM jdk 1.7 :

/**
 * Returns the default charset of this Java virtual machine.
 *
 * <p> The default charset is determined during virtual-machine startup and
 * typically depends upon the locale and charset of the underlying
 * operating system.
 *
 * @return  A charset object for the default charset
 *
 * @since 1.5
 */
public static Charset defaultCharset() {
    if (defaultCharset == null) {
        synchronized (Charset.class) {
            String csn = AccessController.doPrivileged(
                new GetPropertyAction("file.encoding"));
            Charset cs = lookup(csn);
            if (cs != null)
                defaultCharset = cs;
            else
                defaultCharset = forName("UTF-8");
        }
    }
    return defaultCharset;
}

As you know, you could always set file.encoding at startup..

java -Dfile.encoding=UTF-8 MyClass

In your case, SJIS seems refer to a Japan encoding (MS Japan OS src) ? I mean SJIS could be default value when LANG=ja_JP.PCK (src)

-1

I am sure that this is JVM implemenation specific, but I was able to "influence" my JVM's default file.encoding by executing: export LC_ALL=en_US.UTF-8 (running java version = 1.7.0_80 on OS = Ubuntu 12.04)

all the credit goes to http://www.philvarner.com/2009/10/24/unicode-in-java-default-charset-part-4/

  • Please quote the relevant information from your link – Ramhound Jul 10 '16 at 0:08

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.