使用 SBT 构建 scala 无法导入 java 类? [英] scala build with SBT cannot import java classes?
问题描述
我在使用 sbt 构建一个简单的 Scala 类时遇到了一些奇怪的问题.我从互联网上复制的代码足以启动码头服务器:
I got some strange problems when using sbt to build a simple Scala class. The code is simply enough which I copied from internet to start a jetty server:
object JettyEmbedded {
def main(args: Array[String]) {
val server = new org.eclipse.jetty.server.Server(9080)
val context= new org.eclipse.jetty.webapp.WebAppContext()
context.setServer(server)
context.setContextPath("/");
context.setWar("src/main/webapp")
server.setHandler(context);
try {
server.start()
server.join()
} catch {
case e: Exception => {
e.printStackTrace()
System.exit(1)
}
}
}
}
为了使用 SBT 和 scala 2.9.2 进行编译,我收到以下错误:
to compile with SBT and scala 2.9.2, i got the following error:
[error] /src/main/scala/JettyEmbedded.scala:4: object eclipse is not a member of package org
[error] val server = new org.eclipse.jetty.server.Server(9080)
我可以在 build.sbt 中看到,我有如下依赖项:
I can see in build.sbt, i have dependencies like:
"org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
如果我只是删除这个有问题的文件并使用 container:start from sbt 运行 Jetty 服务器,它运行没有问题!
If I just remove this offending file and run the Jetty server with container:start from sbt, it runs no problem!
推荐答案
更改这一行:
"org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container",
到:
"org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container;provided",
解决了这个问题.
sbt 不要将容器"类放入编译类路径中,而在 Eclipse 中它不在乎.
sbt don't put "container" class into the compilation classpath, while in Eclipse it don't care.
这篇关于使用 SBT 构建 scala 无法导入 java 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!