gpt4 book ai didi

java - 您如何指定/确定运行 fat jar 所需的最低 JDK?

转载 作者:行者123 更新时间:2023-12-01 14:20:37 24 4
gpt4 key购买 nike

我在一个项目上使用了 sbt-assembly,我有一些 java 14 jar,我的本地机器有 JDK 8 作为默认 JDK。

sbt assembly 任务成功,生成了一个 fat jar。

当我使用 JDK 8 运行它时,出现错误:

Exception in thread "main" java.lang.UnsupportedClassVersionError: javafx/event/EventTarget has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

JDK 11(55.0 版)是我需要的。果然,当我在 shell 上设置 JDK 11 时,我可以运行 fat jar。

有没有办法在 build.sbt 文件中明确说明目标 JDK 版本?另外,令我惊讶的是,即使我在依赖项中有 Java 14 jar,该应用程序在 JDK 11 上运行良好。这只是 Java 最高向后兼容性的一个例子吗?我想知道还有什么可以起作用。

这就是我的 build.sbt 的样子

name := "scalafx-app"

version := "0.1"

scalaVersion := "2.13.3"

scalacOptions += "-Ymacro-annotations"

useCoursier := false
assemblyMergeStrategy in assembly := {
case "module-info.class" => MergeStrategy.concat
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.1.1"

lazy val osName = System.getProperty("os.name") match {
case n if n.startsWith("Linux") => "linux"
case n if n.startsWith("Mac") => "mac"
case n if n.startsWith("Windows") => "win"
case _ => throw new Exception("Unknown platform!")
}

lazy val javaFXModules = Seq("base", "controls", "fxml", "graphics", "media", "web")


lazy val root = (project in file("."))
.settings(
libraryDependencies += scalaTest % Test,

// scalafx
libraryDependencies += "org.scalafx" %% "scalafx" % "14-R19",
libraryDependencies ++= javaFXModules.map(m =>
"org.openjfx" % s"javafx-$m" % "14.0.1" classifier(osName) withJavadoc()
),
libraryDependencies += "org.scalafx" %% "scalafxml-core-sfx8" % "0.5",

// javafx custom components
libraryDependencies += "com.jfoenix" % "jfoenix" % "9.0.9",
libraryDependencies += "org.kordamp.ikonli" % "ikonli-javafx" % "11.4.0",
libraryDependencies += "org.kordamp.ikonli" % "ikonli-material-pack" % "11.4.0",

// json parsing
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.0",
libraryDependencies += "com.squareup.moshi" % "moshi" % "1.9.3",

// logging
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2",
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3",

)

最佳答案

JAR 就像类的压缩包,每个类都是您可以使用 javap 检查哪个 JDK 版本的类他们需要查看“主要版本” 字段的值;见this .

如果您想确保类被编译为特定的Java 版本,您可以使用release & target scalac options .

像这样:

// Ensure the compiler emits Java 8 bytecode.
scalacOptions ++= Seq("-release", "8", "-target:8")
  • release 用于指定使用哪个Java sdtlib
  • target 用于指定发出哪个 bytcode 版本。

关于java - 您如何指定/确定运行 fat jar 所需的最低 JDK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63397117/

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