Question

I am writing a Java library for the work with geo-coordinates and the tests are implemented with specs2 in Scala. I have many tests that do String comparisons against Strings that include the degree symbol ° (which is a non-ASCII character).

If I run these tests from within IntelliJ, they all pass. They also pass on Travis CI. But if I run sbt test (sbt 11.3) from my Power Shell (Windows x64), all those tests fail and the console shows malformed Strings like shown in the screen shot:

encoding problems

What could be the issue, and how can I fix it? I checked that the files are UTF8-encoded. Also note that changing my Java configuration would not help much, since the tests just have to run if someone else clones the repository (so any solution that solves the problem only on my system isn't going to help). But I have absolutely no clue what is going wrong here...

Was it helpful?

Solution

Are your degree symbols encoded directly in the source file?

If so, I suspect you may have some encoding issues (possibly source files are being interpreted as UTF-8 in IntelliJ and CP1252 when compiled by SBT).

You could either try changing the degree symbols to '\u00B0' and recompiling, or alternatively you could try setting the source file encoding in SBT with javacOptions ++= Seq("-encoding", "UTF-8").

FWIW, the compiler (and runtime) uses the default encoding for source (and other) file I/O, unless told otherwise. The default encoding for Windows is CP1252.

You can tell the javac compiler to use an alternative encoding with the -encoding UTF-8 option, and the runtime with -Dfile.encoding=UTF-8.

Through Google, I also found some information on how to do this on a system-wide basis here (untested by me).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top