BefittingTheorem
BefittingTheorem

Reputation: 10629

Include jar file in Scala interpreter

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.

Upvotes: 28

Views: 22167

Answers (7)

Joe
Joe

Reputation: 85

"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
    

Upvotes: 0

SparkZeus
SparkZeus

Reputation: 358

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.

Upvotes: 1

Eastsun
Eastsun

Reputation: 18859

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)

Upvotes: 27

Amit_P
Amit_P

Reputation: 41

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.

Upvotes: 3

idonnie
idonnie

Reputation: 1703

Include multiple jars int Scala REPL 2.10.0-RC2

scala -classpath my_1st.jar:my_2nd.jar:my_3rd.jar

Upvotes: 6

Ehecatl
Ehecatl

Reputation: 122

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

Upvotes: 3

Nikolay Ivanov
Nikolay Ivanov

Reputation: 8935

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

Upvotes: 22

Related Questions