Search code examples
scalaclasspathread-eval-print-loopartifact

Making jar files available to the scala REPL


I can't seem to get this to work, so i have tried what's probably the most straightforward case--ie, retrieve the jar file and into a given directory then start the scala REPL from that directory--still no luck.

I know the jar files are fine and correctly references (eg, i can access them from project directories via eclipse/scala ide)

I also know that my import statements (one is shown below) are valid.

cd ~/my_scala_jars

retrieve the artifact from the maven repo

wget -q -nd -np -r \
    http://repo1.maven.org/maven2/org/scalaz/scalaz-core_2.12.0-M1/7.1.2/scalaz-core_2.12.0-M1-7.1.2.jar

start the scala REPL and add the jar to the REPL classpath:

scala -cp scalaz-core_2.12.0-M1-7.1.2.jar

scala> import scalaz.stream.io
<console>:7: error: object stream is not a member of package scalaz
   import scalaz.stream.io

in case it's useful, trying this with different jars (that i downloaded as i showed with scalaz above) will sometimes cause the REPL to throw a not found error instead

scala> import breeze.linalg._ 
<console>:7: error: not found: value breeze
    import breeze.linalg._

it seems to me that the REPL should recognize these jars because the directory has been added to my CLASSPATH, but also because i believe the current directory is also added to the classpath (at least during the REPL session)--and still both fail.


Solution

  • I am not sure why the jar files are not picked up as I haven't really tried to do that. What I do, and find useful, is to create a number of build.sbt files for specific purposes, e.g. for working with a specific suite of libraries at a REPL.

    If you do this, and then launch a REPL using the command sbt console, from the directory containing the appropriate build.sbt, then you can have a console with the libraries you want available on the classpath to that console.

    This might not be ideal, but it does work and I find it quite useful.