Scala-Redis 库不包含在 fat jar 文件中

标签 scala redis executable-jar

我正在开发一个更新 Redis 服务器的 scala 独立应用程序(将此应用程序作为 Spark 作业执行)。我的应用程序的 Scala 版本是 2.10。

我正在使用 "net.debasishg"% "redisclient_2.10"% "2.13"与 Redis 服务器一起工作。

我通过 IntelliJ idea 运行应用程序,应用程序运行正常,没有任何错误。然后我通过给出“activator package”命令创建我的应用程序的 jar。然后我执行jar文件,报错如下,

Exception in thread "main" java.lang.NoClassDefFoundError: com/redis/RedisClient
at Main$.main(Main.scala:55)
at Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:328)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) 
Caused by: java.lang.ClassNotFoundException: com.redis.RedisClient
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more

请任何人知道如何克服这个问题。 (不幸的是,我尝试使用 sbt-assembly 插件,但它没有解决我的问题)

谢谢

最佳答案

我已经解决了这个问题,如下所示,

  1. 创建一个 assembly.sbt 文件并添加到应用程序的项目目录中。此文件包含在 "org.apache.spark"%% "spark-core"% "1.0.0"插件中。

  2. 在 build.sbt 文件中添加了以下行

    scalaVersion := "2.10.4"

    libraryDependencies ++= Seq(
     "org.scalatest" %% "scalatest" % "2.1.6" % "test",
     ("org.apache.spark" %% "spark-core" % "1.0.0").
      exclude("org.eclipse.jetty.orbit", "javax.servlet").
      exclude("org.eclipse.jetty.orbit", "javax.transaction").
      exclude("org.eclipse.jetty.orbit", "javax.mail").
      exclude("org.eclipse.jetty.orbit", "javax.activation").
      exclude("commons-beanutils", "commons-beanutils-core").
      exclude("commons-collections", "commons-collections").
      exclude("commons-collections", "commons-collections").
      exclude("com.esotericsoftware.minlog", "minlog"),
     ("org.apache.hadoop" % "hadoop-client" % " 2.2.0").
      exclude("com.twitter", "parquet-column"),
     "org.apache.spark" % "spark-sql_2.10" % "1.2.0",
     "org.apache.spark" % "spark-hive_2.10" % "1.2.0",
     "net.debasishg" % "redisclient_2.10" % "2.14"
     excludeAll(
      ExclusionRule(organization = "org.slf4j"),
      ExclusionRule(organization = "com.twitter")
     ) 
    )
    
    mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) => {
      case PathList("javax", "servlet", xs@_*) => MergeStrategy.first
      case PathList(ps@_*) if ps.last endsWith ".html" => MergeStrategy.first
      case PathList(ps@_*) if ps.last endsWith ".properties" => MergeStrategy.first
      case PathList(ps@_*) if ps.last endsWith ".xml" => MergeStrategy.first
      case PathList(ps@_*) if ps.last endsWith ".class" => MergeStrategy.first
      case PathList(ps@_*) if ps.last endsWith ".thrift" => MergeStrategy.first
      case "application.conf" => MergeStrategy.concat
      case "unwanted.txt" => MergeStrategy.discard
      case x => old(x)
     }
    }
    
  3. 最后使用“activator clean”命令清理应用程序,然后运行“activator assembly”打包项目

谢谢

关于Scala-Redis 库不包含在 fat jar 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695643/

相关文章:

java - 使用 proguard 的 Spring 启动无法创建可执行 jar

scala - 什么情况会抛出异常 "scala.util.control.BreakControl"?

list - 在Scala中将元素添加到列表中

Scala:除了空选项之外,将 None 用于其他目的

Laravel 缓存剩余 TTL

go - 为什么 lpop 会增加 Redis CPU 使用率?

asp.net-core - 如何存储具有到期日期的对象

java - Jar 可以双击运行,但不能在命令行中运行

scala - 通过使用数据帧中多个其他列的值向数据帧添加新列 - spark/scala

java - Netbeans dist jar 文件给出 NoClassDefFoundError