27

Is it possible to include a jar file run running the Scala interpreter?

My code is working when I compile from scalac:

scalac script.scala -classpath *.jar

But I would like to be able to include a jar file when running the interpreter.

BefittingTheorem
  • 10,459
  • 15
  • 69
  • 96

7 Answers7

26

In scala2.8,you can use

scala>:jar JarName.jar

to add a jar to the classpath.

In Scala 2.8.1, it is not :jar but :cp

And in Scala 2.11.7 it is not :cp but :re(quire)

dr jerry
  • 9,768
  • 24
  • 79
  • 122
Eastsun
  • 18,526
  • 6
  • 57
  • 81
  • 3
    I tried this and got a kind of success message.. but it did not work. Added '/shared/spark-core_2.10-0.9.0-incubating-SNAPSHOT.jar'. Your new classpath is: ".:/shared/spark-core_2.10-0.9.0-incubating-SNAPSHOT.jar". But the import did not work. (NB: I verified the import works when adding via command line classpath) – WestCoastProjects Jan 29 '14 at 18:35
  • Same with javadba: import doesn't work when a jar is added to classpath in interpreter, and you will have an error message *"object xxx is not a member of package yyy"* when doing that. – lcn Feb 04 '14 at 19:29
22

According to scala executable help all options of scalac are allowed , so you can run scala -classpath some.jar, i've just tried and it looks like it works

Nikolay Ivanov
  • 8,897
  • 3
  • 31
  • 34
6

Include multiple jars int Scala REPL 2.10.0-RC2

scala -classpath my_1st.jar:my_2nd.jar:my_3rd.jar
idonnie
  • 1,703
  • 12
  • 11
3

in my case i am using Scala code runner version 2.9.2. and i had to add quotation marks. I am using this jar files:

jdom-b10.jar, rome-0.9.jar

and everything goes fine with this:

scala -classpath "*.jar" feedparser.scala
Ehecatl
  • 122
  • 1
  • 6
2

In Scala version 2.11.6 from scala REPL use :require, can best be figured out by using :help from REPL

For example:

$ scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :require lift-json_2.11-3.0-M5-1.jar
Added '<path to lift json library>/lift-json/lift-json_2.11-3.0-M5-1.jar' to classpath.
o11c
  • 15,265
  • 4
  • 50
  • 75
Amit_P
  • 31
  • 2
1

Scala version 2.11.5:

Here is an example of adding all jars in your ivy cache:

scala -cp /Users/dbysani/.ivy2/cache/org.apache.spark/spark-streaming_2.10/jars/*

scala> import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.StreamingContext

You can also create a local folder of all the jars that you need to get added and add it in a similar way.

Hope this helps.

Matt G
  • 2,373
  • 1
  • 14
  • 12
SparkZeus
  • 338
  • 3
  • 5
0

"lib/*.jar" generates a list with blank between items not ":" or ";" as required. Since Java 6 "lib/*" should work, but sometimes doesn't (classpath is set somewhere else)

I use a script like:

  1. Windows:

    @rem all *.jars in lib subdirectory
    
    @echo off
    
    set clp=.
    
    for %%c in (lib\*.jar) do call :Setclasspath %%c
    
    echo The classpath is %clp% 
    
    scala -classpath %clp% script.scala
    
    exit /B %ERRORLEVEL%
    
    :Setclasspath
    set clp=%clp%;%~1
    exit /B 0
    
  2. Linux:

    #!/bin/bash
    
    #all *.jars in lib subdirectory
    
    clp="."
    
    for file in lib/*
    do
      clp="$clp:$file"
    done
    
    echo $clp
    
    
    scala -classpath $clp script.scala
    
Joe
  • 85
  • 6