将.jar文件导入Scala环境 [英] Importing .jar files into Scala environment
问题描述
即使在阅读之后: Scala,jar文件有问题,我还是有点困惑.我试图将一些包导入我的Scala文件,即使添加到类路径后,解释器也无法识别它们.
Even after reading: Scala, problem with a jar file, I'm still a bit confused. I am trying to import some packages into my Scala file, and the interpreter is not recognizing them even after adding to classpath.
一个例子:
我有导入语句:
import org.json4s._
我从此处下载了.jar: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4
I downloaded the .jar from here: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4
并使用以下命令添加到解释器类路径:
and added to the interpreter classpath using:
scala> :cp /Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar
Scala确认类路径:
Scala acknowledges the classpath:
Your new classpath is: ".:/Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar:/Users/aspangher13/Downloads/jna-3.5.2.jar"
但仍会引发此错误:
<console>:7: error: object scalatra is not a member of package org
import org.json4s._
有人可以看到我在做什么错吗?谢谢!
Can anyone see what I'm doing wrong? Thanks!!
作为后续,有人知道在哪里找到该软件包:JsonAST ._?
And as a followup, does anyone know where to find the package: JsonAST._?
推荐答案
简单一点,创建一个sbt小项目.
Go the simple and create a little sbt project.
第一步-创建一个项目
出于您的目的,您不需要复杂的构建.因此,只需创建两个文件:
For your purposes you don't need a complex build. So just create two files:
./build.sbt
name := "name your project"
version := "0.1"
scalaVersion := "2.10.2" // or whatever you prefer
./project/build.properties
sbt.version=0.12.4
只需转到项目根文件夹并调用sbt
The just go to the project root folder and call sbt
第二步-添加依赖项
打开您的./build.sbt
文件并添加:
libraryDependency ++= Seq(
"org.scalatra" %% "scalatra" % "2.2.1",
"org.scalatra" %% "scalatra-scalate" % "2.2.1",
"org.scalatra" %% "scalatra-specs2" % "2.2.1" % "test",
"org.json4s" %% "json4s-native % "3.2.4",
"net.java.dev.jna" & "jna" & "3.5.2"
)
第三步-运行控制台
不要忘记用reload
任务重新加载sbt,然后调用console
或console-quick
任务.这应该起作用.
Don't forget to reload sbt with reload
task, and then call console
or console-quick
task. This should work.
但是有更简单的方法可以做到这一点:
But there are easier ways to do this:
1)使用gitter8- Scalatra gitter8项目
2)深入了解 Scalatra sbt依赖项
1) Use gitter8 - Scalatra gitter8 project
2) Read little into about Scalatra sbt dependencies
这篇关于将.jar文件导入Scala环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!