English 中文(简体)
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.

最佳回答

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

问题回答

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)

Include multiple jars int Scala REPL 2.10.0-RC2

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

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

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.

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.

"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
    




相关问题
Refactor packages in a Jar

I have a requirement that i need to load two versions of a jar at once. To avoid class path collisions I d like to rename the packages of one of the jars. Then in source you could always easily ...

Directory list of JAR file within classpath

The typically method to reference a file contained with a JAR file is to use ClassLoader.getResource. Is there a way to get the contents of a directory within a JAR files (similar to java.io.File....

Including dependencies in a jar with Maven

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file? I have a project the builds into a single jar file. I want the classes from dependencies to be copied into ...

Jar file resource not available?

My program has the following lines, works fine when run from Netbeans, JButton Button_1=new JButton(new ImageIcon(Phone_Dialer.class.getResource("Dir_Icons/"+"Key_1"+Botton_Color+".gif"))); But ...

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 ...

loading from JAR files during deployment vs development

when i am loading some data into my java program, i usually use FileInputStream. however i deploy the program as a jar file and webstart, so i have to use getRessource() or getRessourceAsStream() to ...

热门标签