【问题标题】:Importing in Scala - How to add a jar to the classpath permanently在 Scala 中导入 - 如何将 jar 永久添加到类路径中
【发布时间】:2014-07-15 17:55:36
【问题描述】:

所以我在 scala 中导入包时遇到问题。我从 github 下载了包 breeze,因为我想从概率分布中采样。

我习惯了 Python,我可以在其中下载一个包,将其包含在路径中,然后将其导入代码中。所以我对使用单独的“构建工具”来使用 3rd 方包的想法很陌生。

所以我从github下载了“breeze”源代码,安装了sbt,然后在breeze的源代码中,我运行了sbt,然后我使用“assembly”命令为breeze获取了一个.jar。

如果我想使用 scala 解释器,我可以使用

很好地导入包
   scala -cp breeze-master/target/scala-2.11/breeze-parent-assembly-0.8.jar

问题是我想在单独的一段代码中使用这个包,我在一个名为 Chromosome.scala 的文件中编写。当我尝试导入包时(如下所示),出现错误:

    error: not found: object breeze

这是我的代码:

// Chromosome.scala

import breeze.stats.distributions._

class Chromosome(s:Int, bitstring:Array[Boolean]) {
  val size:Int = s;
  val dna:Array[Boolean] = bitstring;
  var fitness:Int = 0;

  def mutate(prob:Float):Unit = {
    // This method will randomly "mutate" the dna sequence by flipping a bit.
    // Any individual bit may be flipped with probability 'pm', usually small.

    val pm:Float = prob;

    // The variable bern is an instance of a Bernoulli random variable,
    // whose probability parameter is equal to 'pm'.
    var bern = new Bernoulli(pm);

    //Loop through the 'dna' array and flip each bit with probability pm.
    for (i <- 0 to (size - 1)) {
      var flip = bern.draw();
      if (flip) {
        dna(i) = !(dna(i));
      }
    }
  }

【问题讨论】:

  • 请包含您的脚本和构建代码。听起来这里可能存在一些误解,但如果没有具体细节,就很难说出来。
  • 感谢您的回复 - 刚刚编辑了帖子以更具体并包含一些代码。

标签: scala import package sbt scala-breeze


【解决方案1】:

“脚本?”这是什么,它与您的 SBT 项目有什么联系? Scala 脚本包括它们自己的用于 Scala 解释器/编译器 (/REPL...) 的启动命令。如果您想访问标准库之外的内容,则必须将它们包括在内。或者,您可以使用SBT Start Script plug-in 生成包含项目依赖项的启动器脚本。它只能在本地工作,但您可以编写一些文本处理和其他 shell 脚本来生成可移植的启动包。

【讨论】:

    【解决方案2】:

    看起来对于 sbt 应该为你做什么有一些可以理解的困惑。

    首先,您通常不需要从 github 下载包并从源代码构建它。在极少数情况下(例如,当您需要尚未发布到库中的功能时),sbt 可以处理繁重的工作。

    相反,你告诉 sbt 一些你正在构建的项目(包括它的依赖项),sbt 将下载它们,编译你的代码,并为 scala 解释器设置运行时类路径(在无数其他构建中相关任务)。

    只需关注the directions on the breeze wiki。具体来说,在项目的根文件夹中创建一个build.sbt 文件并将其复制到其中:

    libraryDependencies  ++= Seq(
                // other dependencies here
                "org.scalanlp" % "breeze_2.10" % "0.7",
                // native libraries are not included by default. add this if you want them (as of 0.7)
                // native libraries greatly improve performance, but increase jar sizes.
                "org.scalanlp" % "breeze-natives_2.10" % "0.7",
    )
    
    resolvers ++= Seq(
                // other resolvers here
                // if you want to use snapshot builds (currently 0.8-SNAPSHOT), use this.
                "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
                "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
    )
    
    // Scala 2.9.2 is still supported for 0.2.1, but is dropped afterwards.
    // Don't use an earlier version of 2.10, you will probably get weird compiler crashes.
    scalaVersion := "2.10.3"
    

    将您的源代码放入适当的文件夹(默认为src/main/scala)并运行sbt console。此命令将下载依赖项、编译代码并启动 Scala 解释器。此时,您应该可以与您的班级互动了。

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2012-04-22
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多