使用scopt OptionParser和Spark时的NoClassDefFoundError

时间:2015-03-29 00:36:43

标签: scala apache-spark noclassdeffounderror scopt

我正在使用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页面中列出的解决方案有助于解决此错误。基本上在项目/目录中创建文件assembly.sbt并添加该行 addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")

请注意,应根据使用的版本添加版本。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

如果您正在使用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>