How do I import a package into Scala's REPL?
I am trying to import this package called funsets which has an object named "FunSets".
I tried several variations of import funsets._
and import funsets._;
etc but it is still not importing the functions and object in the package.

- 569
- 1
- 9
- 21
-
What is funsets? Is it in a jar? Is it a compiled class file? Is it a scala file? Is it a managed dependency in an external repository like maven central? – Dylan Nissley Dec 18 '17 at 19:35
-
funsets is the name of the package that has an object called FunSets in it. It is just in a scala object file. – Linkx_lair Dec 18 '17 at 19:41
-
1You need to run `console` from *within the sbt shell*. Then, sbt will compile whatever is defined in funsets and automatically add it in your Scala shell. From there, you'll be able to do `import funsets._`. – Jorge Dec 19 '17 at 23:35
3 Answers
One way is to compile the "scala classes" and put those in classpath
.
Example,
1) Say you have a class funsets.FunSets.scala
package funsets
object FunSets {
def fun = "very fun"
}
2) Compile the class first using scalac
. (If you use sbt
then sbt compile
would put compiled classes in target/
folder)
scalac FunSets.scala
You will see the funsets
folder/package created,
$ ls -l
total 16
-rw-r--r-- 1 updupd NA\Domain Users 63 Dec 18 11:05 FunSets.scala
drwxr-xr-x 4 updupd NA\Domain Users 136 Dec 18 11:06 funsets
3) Then start REPL with funsets
package in classpath
$ scala -classpath .
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.
scala> import funsets._
import funsets._
Note: if you use sbt compile
, put target/classes
in classpath.
Access Funsets singleton,
scala> FunSets.fun
res0: String = very fun
Also read Scala REPL unable to import packge

- 30,204
- 14
- 155
- 192
You need to add the appropriate jar(s) to the classpath as well using the -cp <jar files>
argument when starting the repl. Alternatively you can use the :require <jar file>
directive from the repl to load a jar after you've already started the session.

- 8,400
- 1
- 16
- 31
Assuming you have some code in a scala file, you can load external scala files from the scala repl using the :load
command. More info in this answer.

- 594
- 1
- 5
- 12
-
In the REPL, I tried doing `:load "C:...full file path name` and whole bunch of variations. I even tried doing `:paste "full file path name" But it keeps saying like "usage: :load -v file" or "usage: :paste -raw file" – Linkx_lair Dec 18 '17 at 20:26
-
1If you start the repl in the same directory as your file you should be able to just do `:load myFile.scala`. – Dylan Nissley Dec 18 '17 at 21:27
-
1According to [this answer](https://stackoverflow.com/questions/35070783/load-a-scala-file-from-command-line), you might have to use the `:lo` command instead of `:load`, in windows even though it should have the same behavior. – Dylan Nissley Dec 18 '17 at 21:29