【问题标题】:NoClassDefFoundError while using scopt OptionParser with Spark将 scopt OptionParser 与 Spark 一起使用时出现 NoClassDefFoundError
【发布时间】:2015-06-02 04:13:29
【问题描述】:

我使用的是 Apache Spark 1.2.1 版和 Scala 2.10.4 版。我试图让示例MovieLensALS 工作。但是,我遇到了 scopt 库的错误,这是代码中的要求。任何帮助,将不胜感激。 我的 build.sbt 如下:

name := "Movie Recommender System"

version := "1.0"

scalaVersion := "2.10.4"

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.2.1"

libraryDependencies += "org.apache.spark" %% "spark-graphx" % "1.2.1"

libraryDependencies += "org.apache.spark"  % "spark-mllib_2.10" % "1.2.1"

libraryDependencies += "com.github.scopt" %% "scopt" % "3.2.0"

resolvers += Resolver.sonatypeRepo("public")

我得到的错误如下:

   Exception in thread "main" java.lang.NoClassDefFoundError: scopt/OptionParser
    at MovieLensALS.main(MovieLensALS.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:358)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)

    Caused by: java.lang.ClassNotFoundException: scopt.OptionParser
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    ... 8 more
    Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties

在运行 sbt 程序集构建 jar 时,我收到以下错误:

[error] Not a valid command: assembly
[error] Not a valid project ID: assembly
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: assembly
[error] assembly
[error]         ^

编辑:根据 Justin Piphony 的建议,在 sbt 的 GitHub 页面中列出的解决方案有助于修复此错误。基本上在 project/ 目录中创建一个文件 assembly.sbt 并添加该行 addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")

注意版本要根据使用的版本添加。

【问题讨论】:

  • 你是如何构建你的 jar 的?
  • @JustinPihony 使用 sbt 包

标签: scala apache-spark noclassdeffounderror scopt


【解决方案1】:

您需要将scopt 打包到您的jar 中。 sbt 默认不这样做。要创建这个胖罐子,你需要使用sbt-assembly

【讨论】:

  • 用 sbt 程序集的细节编辑了问题
  • 您是否按照您的 sbt 版本的设置进行操作。好像插件没有正确安装。
  • 你能提供一些细节吗?我下载了 sbt 工具并安装在我的盒子里。
  • 详细信息在 sbt-assembly 的 github 页面上。根据版本有 3 种可能性。所以你需要检查你有哪个版本
【解决方案2】:

如果你使用maven打包你的spark项目,你需要添加maven-assembly-plugin插件来帮助打包依赖:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5</version>
<configuration>
  <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
  </descriptorRefs>
</configuration>
<executions>
  <execution>
    <id>make-assembly</id>
    <!-- this is used for inheritance merges -->
    <phase>package</phase>
    <!-- bind to the packaging phase -->
    <goals>
      <goal>single</goal>
    </goals>
  </execution>
</executions>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多