简体   繁体   English

sbt-assembly - 在 jar 中找不到主类

[英]sbt-assembly - can not find main class in jar

I have a project where I am trying to create a fat jar using the sbt-assembly plugin.我有一个项目,我正在尝试使用 sbt-assembly 插件创建一个胖 jar。 When I attempt to run my main class from the jar using the java -jar command, I get the error message: Error: Could not find or load main class com.gordon.timeshare.apps.TimeShareLauncher .当我尝试使用java -jar命令从 jar 运行我的主类时,我收到错误消息: Error: Could not find or load main class com.gordon.timeshare.apps.TimeShareLauncher

I only have one main class in my project (I use the extends App syntax to accomplish this), so I do not specify the path to the main class explicitly, although I have tried that and it did not help.我的项目中只有一个主类(我使用extends App语法来实现这一点),所以我没有明确指定主类的路径,尽管我已经尝试过了,但没有帮助。

Below are all the settings I have in my build.sbt file.以下是我在build.sbt文件中的所有设置。

ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.gordon.timeshare.apps"
ThisBuild / scalaVersion := "2.13.5"

lazy val app = (project in file("app"))
  .settings(
    assembly / mainClass := Some("com.gordon.timeshare.apps.TimeShareLauncher"),
    assembly / assemblyJarName := "TimeShareLauncher.jar"
  )

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

I have also tried other strategies like deduplicate , but that would give me an error when trying to make the .jar.我还尝试了其他策略,例如deduplicate ,但是在尝试制作 .jar 时会给我一个错误。

Additionally, when making the .jar, I get a warning:此外,在制作 .jar 时,我收到一条警告:

[warn] Could not create directory C:\Users\dgord\workspace\new-timeshare\timeshare\target\streams\_global\assembly\_global\streams\assembly\88fbe735ce5abc6987fbc59b072404628cdc94b4_a99f2fe2a42747ed9809d4f62f51a9e1b336dde8_da39a3ee5e6b4b0d3255bfef95601890afd80709\META-INF\versions\9: java.nio.file.FileAlreadyExistsException: C:\Users\dgord\workspace\new-timeshare\timeshare\target\streams\_global\assembly\_global\streams\assembly\88fbe735ce5abc6987fbc59b072404628cdc94b4_a99f2fe2a42747ed9809d4f62f51a9e1b336dde8_da39a3ee5e6b4b0d3255bfef95601890afd80709\META-INF\versions\9

And in case you want to know what my main class looks like:如果您想知道我的主要课程是什么样的:

package com.gordon.timeshare.apps

object TimeShareLauncher extends App
  • sbt: 1.4.7 (also tried 1.5.5) sbt:1.4.7(也试过 1.5.5)
  • sbt-assembly: 1.1.0 sbt 组装:1.1.0
  • scala 2.13.5斯卡拉 2.13.5

I have also tried this on WSL and had the same result.我也在 WSL 上尝试过这个并得到了相同的结果。

The issue is with lazy val app = (project in file("app")) .问题在于lazy val app = (project in file("app")) Assuming a single module project with no module named app , sbt-assembly will create a directory named app and attempt to stuff the build in there.假设一个没有名为app模块的单个模块项目, sbt-assembly 将创建一个名为app的目录并尝试在其中填充构建。 However, since the main class is not in the app bundle, the class will not be added to the jar file.但是,由于主类不在app包中,因此不会将该类添加到 jar 文件中。

The correct way to do this is:正确的做法是:

lazy val app = (project in file(".")) , which specifies the current directory as the one to look for the main class. lazy val app = (project in file(".")) ,指定当前目录作为查找主类的目录。 So this was not really an issue with knowing how to use the sbt-assembly plugin, but a more general issue with specifying projects in an sbt build.所以这不是知道如何使用sbt-assembly插件的真正问题,而是在 sbt 构建中指定项目的更普遍的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM