Scala Play 2.0.编译错误:解码时发生IO错误 [英] Scala Play 2.0. Compilation error: IO error while decoding

查看:90
本文介绍了Scala Play 2.0.编译错误:解码时发生IO错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从GitHub( https://github.com/henrikengstrom/roygbiv),而其中一个模块是Play 2.0模块.因此,我可以在每个模块上使用SBT的run命令运行整个应用程序,并且一切正常.但是,当我将非英文字符添加到Play 2.0模板(index.scala.html)并在浏览器中按F5时,会出现编译错误:

I downloaded multi-module Scala project from GitHub (https://github.com/henrikengstrom/roygbiv), and one of the module is Play 2.0 module. So I can run whole application using SBT's run command on each module, and all works fine. But when I add to Play 2.0 template (index.scala.html) non-English characters and press F5 in browser I get compilation error:

解码时发生IO错误 C:\ Users ... \ web \ target \ scala-2.9.1 \ src_managed \ main \ views \ html \ index.template.scala 使用UTF-8时,请尝试使用-encoding指定另一个 选项

IO error while decoding C:\Users...\web\target\scala-2.9.1\src_managed\main\views\html\index.template.scala with UTF-8 Please try specifying another one using the -encoding option

播放2.0模块,我也使用SBT的run命令运行,而不使用Play控制台.

Play 2.0 module I run also using SBT's run command, not using Play console.

我检查了源文件的编码-它是UTF-8.还尝试了不带BOM的UTF-8.

I checked source file encoding - it is UTF-8. Also tried UTF-8 without BOM.

哪里有问题?

推荐答案

您的问题似乎是这样的:您的中间Scala文件未正确编码.

Your problem seems to be this: your intermediate scala files are not encoded correctly.

这里是过程:

Play会获取您的模板文件(foo.scala.html)并将其转换为Scala:target/scala-2.10/src_managed/main/views/html/foo.template.scala.然后通过sbt将其编译为.class文件并通过play运行.

Play takes your template file (foo.scala.html) and translates this into Scala: target/scala-2.10/src_managed/main/views/html/foo.template.scala. This then gets compiled by sbt to .class files and run by play.

当sbt创建这些中间文件时,它将使用默认编码创建它们(在我的情况下是Windows计算机,因此UTF-8不带BOM-您的计算机可能有所不同).重要的是,这种编码始终存在,因此,即使我更改了原始模板文件的编码(将foo.scala.html更改为UTF-16),. scala文件的编码仍然相同(UTF-8中没有BOM)案子).但是,由于scala编译器期望使用ITF-8,因此无法读取该文件,因此不再编译该文件.

When sbt creates these intermediate files, it creates them with the default encoding (in my case a Windows machine so UTF-8 without BOM - your machine may differ). Importantly, this encoding sticks around, so even if I change the encoding of the original template file (foo.scala.html to UTF-16), the encoding of the .scala file is still the same (UTF-8 without BOM in my case). However, the file no longer compiles because the file can't be read because the scala compiler is expecting ITF-8.

正确"的解决方案是始终使用UTF-8,实际上,这是推荐用于play 1.x的解决方案,请参见播放2 .您也可以使用普通的国际化邮件文件.

The 'correct' solution is to always use UTF-8, and in fact this was the solution recommended for play 1.x see Play documentation Internationalization. Here is the equivalent for play 2. You can also use normal internationalization messages files.

因此,如果您指定

JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF8' sbt

Bjorn ,那么这将告诉sbt它读取和写入的所有文件都将位于UTF8中.您还可以在Build.scala中为scala编译器指定文件编码:

as suggested by Bjorn, then this will tell sbt that all files that it reads and writes will be in UTF8. You can also specify the file encoding for the scala compiler in your Build.scala:

val main = play.Project(appName, appVersion, appDependencies).settings(
  scalacOptions ++= Seq("-encoding", "UTF-8")
  // Add your own project settings here      
)

这告诉scala编译器它读取的所有文件(即foo.template.scala)均以UTF-8编码.如果将其设置为默认编码,则也可以正常工作.

This tells the scala compiler that all files that it reads (i.e. the foo.template.scala) are encoded in UTF-8. If you set this to your default encoding, this may work as well.

您最好的选择是清理sbt,确保有问题的文件消失了,然后如上所述使用JAVA_TOOL_OPTION重新启动.但是,您必须确保所有构建都考虑到这一点(詹金斯,其他开发人员等).

Your best bet is to do an sbt clean, ensuring that the offending files have disappeared, and restarting with the JAVA_TOOL_OPTION as suggested above. However, you'll have to ensure that all of your builds take this into account (jenkins, other developers etc).

这篇关于Scala Play 2.0.编译错误:解码时发生IO错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆