Play Framework Unexpected MalformedInputException while Deploying

Few days back while deploying a play application on a ubuntu server, I got following error (this application was running fine in dev and prod mode on a mac)

play.api.UnexpectedException: Unexpected exception[MalformedInputException: Input length = 1]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:276)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
        at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.read1(BufferedReader.java:210)
        at java.io.BufferedReader.read(BufferedReader.java:286)
        at java.io.Reader.read(Reader.java:140)
        at scala.io.BufferedSource.mkString(BufferedSource.scala:96)
    	...

This error is thrown when a string or character in input file is not valid in the concerned charset. By default, the charset is picked from underlying OS and you can print it using Charset.defaultCharset().

Luckily, we can modify the charset without modifying the OS settings by passing file-encoding during java startup. For play application, this can be done at command line using following command:

bin/play-app -Dplay.crypto.secret=<blah> -J-Dfile.encoding=UTF8

To know more about MalformedInputException, you can look at following documentation

play-framework