Import .jar files into Scala

Even after reading: Scala, the problem with the jar file , I'm still a bit confused. I am trying to import some packages into my Scala file, and the interpreter does not recognize them even after adding to the classpath.

One example:

I have an import statement:

import org.json4s._ 

I downloaded .jar from here: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4

and added to the interpreter class path using:

 scala> :cp /Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar 

Scala confirms 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" 

But still it produces this error:

 <console>:7: error: object scalatra is not a member of package org import org.json4s._ 

Can anyone see what I'm doing wrong? Thanks!!

And as a follow-up, does anyone know where to find the package: JsonAST._?

+4
source share
2 answers

Go simple and create a small sbt project.

The first step is to create a project

For your purposes, you do not need a complicated assembly. 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 

Just go to the project root folder and call sbt

Second step - add dependencies

Open the ./build.sbt file and add:

 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" ) 

Step Three - Launch the Console

Remember to restart sbt with the reload task, and then call the console or console-quick task. That should work.

But there are simpler ways to do this:

1) Use gitter8 - Scalatra gitter8 project
2) Get a little familiarity with the dependencies of Scalatra sbt

+5
source

Not sure how: cp works, but if you run

 scala -classpath "list of jars colon separated" 

then inside REPL do your import, it should work

 import org.json4s._ import org.xyz 

However, when you try to use classes, you will probably skip the transitive dependencies required by json4s, and so we return to the sbt @ 4lex1v example, which will handle this. Creating a small project and running sbt console really greatly simplify this.

It seems that -classpath and: cp are intended primarily to make your code available in the shell, and then only if you understand all the transitive dependencies or don't have.

+4
source

Source: https://habr.com/ru/post/1489312/


All Articles