Spiral Arm Logo

Richard's technical notes

Monday, September 21, 2009

Setting the character encoding in Scala

The only reliable way we've found for setting the default character encoding for Scala is to set $JAVA_OPTS before running your application:


$ JAVA_OPTS="-Dfile.encoding=utf8" scala
Welcome to Scala version 2.7.5.final [...]
Type in expressions to have them evaluated.
Type :help for more information.

scala> val x = "garçon"
x: java.lang.String = garçon


Just trying to set scala -Dfile.encoding=utf8 doesn't seem to do it. Of course, you may not need this if your OS defaults to a sensible character encoding. I'm lumbered with something called "MacRoman"...

You'll also want to make sure your terminal is set to UTF-8 encoding, which on the Mac is Terminal -> Preferences -> Settings -> Advanced -> International.

2 Comments:

Blogger happygiraffe said...

That's because scala runs on the JVM. So by the time scala has started and can interpret the command line, it's too late to set the encoding. :-(

Mind you, the whole concept of "default encodings" still annoys me. It's almost guaranteed to create bugs and destroy portability.

I've been looking at this myself this morning. I can't find any sensible definition of what encoding gets used for interpreting the command line arguments on the JVM. Nothing appears to state it explicitly. Experiment shows that -Dfile.encoding does the trick, but it'd be nice to have it backed up by something.

8:16 PM  
Blogger Jono said...

If you want this to work via maven use MAVEN_OPTS rather than JAVA_OPTS, this is useful when testing liftweb applications via the mvn jetty:run command.

1:01 AM  

Post a Comment

<< Home