在 Scala 解释器中包含 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1707454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:43:05  来源:igfitidea点击:

Include jar file in Scala interpreter

scalajarinterpreter

提问by BefittingTheorem

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

是否可以包含运行 Scala 解释器的 jar 文件?

My code is working when I compile from scalac:

当我从 scalac 编译时,我的代码正在运行:

scalac script.scala -classpath *.jar

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

但是我希望能够在运行解释器时包含一个 jar 文件。

采纳答案by Nikolay Ivanov

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

根据 scala 可执行文件帮助,scalac 的所有选项都是允许的,因此您可以运行scala -classpath some.jar,我刚刚尝试过,看起来它可以工作

回答by Eastsun

In scala2.8,you can use

在 scala2.8 中,你可以使用

scala>:jar JarName.jar

to add a jar to the classpath.

将 jar 添加到类路径。

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

在 Scala 2.8.1 中,它不是 :jar 而是 :cp

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

在 Scala 2.11.7 中它不是 :cp 而是 :re(quire)

回答by idonnie

Include multiple jars int Scala REPL 2.10.0-RC2

在 Scala REPL 2.10.0-RC2 中包含多个 jars

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

回答by Ehecatl

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:

就我而言,我使用的是 Scala 代码运行器版本 2.9.2。我不得不加引号。我正在使用这个 jar 文件:

jdom-b10.jar, rome-0.9.jar

jdom-b10.jar、rome-0.9.jar

and everything goes fine with this:

一切顺利:

scala -classpath "*.jar" feedparser.scala

回答by Amit_P

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

在 Scala 版本 2.11.6 中来自 Scala REPL use :require,最好通过使用:help来自 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.

回答by SparkZeus

Scala version 2.11.5:

Scala 版本 2.11.5:

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

这是在常春藤缓存中添加所有 jar 的示例:

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.

您还可以创建一个包含所有需要添加的 jar 的本地文件夹,并以类似的方式添加它。

Hope this helps.

希望这可以帮助。

回答by Joe

"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)

"lib/*.jar" 生成一个列表,项目之间不是 ":" 或 ";" 按要求。由于 Java 6 "lib/*" 应该可以工作,但有时不能(类路径设置在其他地方)

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
    
  1. 视窗:

    @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