【问题标题】:Running a scala program with the java tool / scala main function使用 java 工具 / scala main 函数运行 scala 程序
【发布时间】:2019-08-30 10:36:53
【问题描述】:

我有一个带有 object 的 scala 应用程序,它实现了 def main(args:Array[String]): Unit 特征。我使用sbtAssembly 插件创建了一个胖罐子,我可以使用以下命令从控制台运行它:

scala myjar.jar

但是,我想把它交给没有安装 scala,而只安装 jdk/jre 的同事。所以我尝试用java运行它,但是它失败了:

java -jar myjar.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Seq
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

我必须做什么才能使用java 运行它?这甚至可能吗?

【问题讨论】:

  • 组装插件是如何设置的?它是否排除了 Scala 标准库(类似于github.com/sbt/sbt-assembly#excluding-scala-library-jars)?你能检查一下scala/collection/Seq.class 是否在你的胖罐子里吗?
  • 完美!这确实是缺少的部分,它被设置为includeScala = false。更改为true 后,我可以使用java -jar myjar.jar 运行它。如果您将此提示添加为答案,我可以接受。

标签: java scala command-line jar console-application


【解决方案1】:

要仅使用 java -jar 而不指定类路径来运行 jar,它必须包含所有依赖项,包括标准 Scala 库(从错误消息中我们可以看到缺少的内容)。默认情况下 sbt-assembly 包含它,但它可以配置为to exclude it,这就是你的构建所做的。只需删除includeScala = false(或将其设置为true)。

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 2016-08-17
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2015-08-25
    • 2016-05-12
    相关资源
    最近更新 更多