gpt4 book ai didi

java - sbt 汇编有效,但仍然是 "no main manifest attribute"

转载 作者:行者123 更新时间:2023-11-30 02:20:57 25 4
gpt4 key购买 nike

我正在使用 sbt 1.0.2。我的 build.sbt 是

name := "myprogram"

version := "0.1"

scalaVersion := "2.12.3"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

// https://mvnrepository.com/artifact/net.sf.py4j/py4j
libraryDependencies += "net.sf.py4j" % "py4j" % "0.10.6"

scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-feature",
"-encoding", "UTF-8",
"-Xfuture",
"-Xlint:unsound-match", // Pattern match may not be typesafe.
"-Yno-adapted-args", // Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver.
"-Ypartial-unification", // Enable partial unification in type constructor inference
"-Ywarn-dead-code", // Warn when dead code is identified.
"-Ywarn-inaccessible", // Warn about inaccessible types in method signatures.
"-Ywarn-infer-any", // Warn when a type argument is inferred to be `Any`.
"-Ywarn-nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'.
"-Ywarn-nullary-unit", // Warn when nullary methods return Unit.
"-Ywarn-unused", // Warn if a method or value is unused.
"-Ywarn-unused:implicits", // Warn if an implicit parameter is unused.
"-Ywarn-unused:imports", // Warn if an import selector is not referenced.
"-Ywarn-unused:locals", // Warn if a local definition is unused.
"-Ywarn-unused:params", // Warn if a value parameter is unused.
"-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused.
"-Ywarn-unused:privates", // Warn if a private member is unused.
"-Ywarn-value-discard" // Warn when non-Unit expression results are unused.
)

javacOptions ++= Seq("-source", "1.8", "-target", "1.8")

test in assembly := {}
assemblyJarName in assembly := "myprogram.jar"

exportJars := true

mainClass in Compile := Some("com.company.MyClass")
mainClass in assembly := Some("com.company.MyClass")
mainClass in(Compile, run) := Some("com.company.MyClass")
mainClass in(Compile, packageBin) := Some("com.company.MyClass")

它似乎可以使用sbt assembly编译,其中一些输出是

[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list
[info] Including: py4j-0.10.6.jar
[info] Including: scala-library.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Merging 'META-INF/maven/net.sf.py4j/py4j/pom.properties' with strategy 'discard'
[warn] Merging 'META-INF/maven/net.sf.py4j/py4j/pom.xml' with strategy 'discard'
[warn] Strategy 'discard' was applied to 3 files

一旦我进入命令行,我就会得到

$ java -jar myprogram.jar
no main manifest attribute, in myprogram.jar

我打开我的 jar 并查看MANIFEST.MF,但找不到主要内容:

Manifest-Version: 1.0
Implementation-Title: myprogram
Implementation-Version: 0.1
Specification-Vendor: default
Specification-Title: myprogram
Implementation-Vendor-Id: default
Specification-Version: 0.1
Implementation-Vendor: default

值得一提的是,我的程序主要是用 Scala 编写的,除了我的主类是用 Java 编写的。

我需要改变什么?

最佳答案

从您显示的日志中,我的猜测是 - 在其中一个包含的 jar 的内容中 - 已经有一个 META-INF/MANIFEST.MF ,以及您的/由 sbt 创建的那个程序集 - 声明主类的地方 - 被丢弃(但我不能 100% 确定,因为您显示的 list 内容似乎包含来自代码的内容)。

我认为这表明您的 list 被丢弃:

[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'

我敢打赌 py4j-0.10.6.jar 中已经有一个“META-INF/MANIFEST.MF”。

如何解决这个问题?

  1. 避免使用 fat jar,例如使用 http://www.scala-sbt.org/sbt-native-packager ,但我想这不是重点
  2. 调整合并策略,详情请参见 https://github.com/sbt/sbt-assembly#merge-strategy 。更具体地说,您可以尝试设置用于合并 META-INF/MANIFEST.MFMergeStrategy.concat 策略。

大致:

assemblyMergeStrategy in assembly := {
case PathList("META-INF", "MANIFEST.MF") => MergeStrategy.concat
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}

关于java - sbt 汇编有效,但仍然是 "no main manifest attribute",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46844586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com