Aroon Aroon - 1 month ago 26
Scala Question

How to write the "import scala.io.Source" , "import java.io" libraries in SBT

I am compiling the Scala program using the SBT but It gives me following error for "import scala.io.Source" , "import java.io"

sbt.ResolveException: unresolved dependency: org.scala#scala.io.Source_2.11;latest.integration: not found
[error] unresolved dependency: org.java#java.io_2.11;latest.integration: not found


My SBT format is given below:

name := "Simple Project"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-graphx" % "2.0.1",
"org.scala" %% "scala.io.Source" % "latest.integration",
"org.java" %% "java.io" % "latest.integration"
)


Anybody can help that how i can specify the "import scala.io.Source" , "import java.io" in SBT.

Answer

One needs to make a distinction between library dependencies and package imports: library dependencies are managed through the build system (like sbt or maven or grails,...) and make complete libraries (like a logging API, HTTP implementation, ...) available to the system being built.

At the program level, imports are used to bring specific parts of libraries into the scope of the code being developed.

Given this build.sbt

name := "examplebuild"

version := "0.0.1"

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "com.typesafe" % "config" % "1.2.1",
  "org.scalaj" % "scalaj-http_2.11" % "2.3.0"
)

We can develop a scala program that can use the configuration library from typesafe and the http library from scalaj

Sample.scala

package com.example

import scala.io.Source  // from the Scala standard library
import java.io._  // import all io package from the standard java library

import com.typesafe.ConfigFactory // from the typesafe config library
import scalaj.http._  // import all members of the scalaj.http package

class Sample {
   // code here
}