sbt - Scala - Brew - MacOS - 根本无法连接到 sbt shell:NoClassDefFoundError

sbt - Scala - Brew - MacOS - Unable to connect to sbt shell at all: NoClassDefFoundError

所以我只是想使用 JDK 15 / SBT 1.5.4 / Scala 2.13.6 设置一个项目,所有这些都是通过 brew 在 MacOS 上安装的。

然而,在我什至尝试通过 intellij 等构建之前。我无法通过普通终端连接到 sbt shell。

[info] welcome to sbt 1.5.4 (AdoptOpenJDK Java 15.0.2)
[info] loading global plugins from /Users/user/.sbt/1.0/plugins
[warn] Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? (default: r)
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: Could not initialize class com.swoval.runtime.ShutdownHooks
        at com.swoval.runtime.ShutdownHooks.run(ShutdownHooks.java:25)

我已经尝试重新安装所有三个依赖项几次并清除了缓存,但仍然没有。我猜有些东西配置不正确,因为它甚至无法连接到 shell.

谢谢!

编辑:我刚刚按照 sbt 文档上的安装步骤进行操作,唯一的要求是 JDK 11 或 8 和 sbt 本身。这次我已经删除了 scala / JDK15 并通过 SDK-man 重新下载了 JDK 11,但我仍然遇到同样的问题。

编辑 2:我删除了 .sbt 目录并重新启动,现在 sbt 命令显示更多输出,希望这能有所帮助。

java.lang.NoClassDefFoundError: Could not initialize class com.swoval.runtime.ShutdownHooks
    at com.swoval.runtime.NativeLoader.loadPackaged(NativeLoader.java:143)
    at com.swoval.runtime.NativeLoader.loadPackaged(NativeLoader.java:174)
    at com.swoval.files.apple.FileEventMonitorImpl.<clinit>(FileEventMonitors.java:127)
    at com.swoval.files.apple.FileEventMonitors.get(FileEventMonitors.java:47)
    at com.swoval.files.ApplePathWatcher.<init>(ApplePathWatcher.java:258)
    at com.swoval.files.ApplePathWatcher.<init>(ApplePathWatcher.java:194)
    at com.swoval.files.ApplePathWatchers.get(ApplePathWatcher.java:331)
    at com.swoval.files.PathWatchers.get(PathWatchers.java:84)
    at com.swoval.files.FileTreeRepositories.get(FileTreeRepositories.java:64)
    at com.swoval.files.FileTreeRepositories.get(FileTreeRepositories.java:32)
    at sbt.internal.nio.FileTreeRepositoryImpl.<init>(FileTreeRepositoryImpl.scala:46)
    at sbt.internal.nio.FileTreeRepository$.default(FileTreeRepository.scala:40)
    at sbt.BuiltinCommands$.$anonfun$setupGlobalFileTreeRepository(Main.scala:985)
    at sbt.BuiltinCommands$.$anonfun$doLoadProject(Main.scala:974)
    at sbt.Project$.setProject(Project.scala:501)
    at sbt.BuiltinCommands$.doLoadProject(Main.scala:974)
    at sbt.BuiltinCommands$.$anonfun$loadProjectImpl(Main.scala:912)
    at sbt.Command$.$anonfun$applyEffect(Command.scala:150)
    at sbt.Command$.$anonfun$applyEffect(Command.scala:145)
    at sbt.Command$.process(Command.scala:189)
    at sbt.MainLoop$.$anonfun$processCommand(MainLoop.scala:245)
    at scala.Option.getOrElse(Option.scala:189)
    at sbt.MainLoop$.process(MainLoop.scala:245)
    at sbt.MainLoop$.processCommand(MainLoop.scala:278)
    at sbt.MainLoop$.$anonfun$next(MainLoop.scala:163)
    at sbt.State$StateOpsImpl$.runCmd(State.scala:289)
    at sbt.State$StateOpsImpl$.process$extension(State.scala:325)
    at sbt.MainLoop$.$anonfun$next(MainLoop.scala:163)
    at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:23)
    at sbt.MainLoop$.next(MainLoop.scala:163)
    at sbt.MainLoop$.run(MainLoop.scala:144)
    at sbt.MainLoop$.$anonfun$runWithNewLog(MainLoop.scala:119)
    at sbt.io.Using.apply(Using.scala:27)
    at sbt.MainLoop$.runWithNewLog(MainLoop.scala:112)
    at sbt.MainLoop$.runAndClearLast(MainLoop.scala:66)
    at sbt.MainLoop$.runLoggedLoop(MainLoop.scala:51)
    at sbt.MainLoop$.runLogged(MainLoop.scala:42)
    at sbt.StandardMain$.runManaged(Main.scala:218)
    at sbt.xMain$.$anonfun$run(Main.scala:133)
    at scala.util.DynamicVariable.withValue(DynamicVariable.scala:62)
    at scala.Console$.withIn(Console.scala:230)
    at sbt.internal.util.Terminal$.withIn(Terminal.scala:560)
    at sbt.internal.util.Terminal$.$anonfun$withStreams(Terminal.scala:350)
    at scala.util.DynamicVariable.withValue(DynamicVariable.scala:62)
    at scala.Console$.withOut(Console.scala:167)
    at sbt.internal.util.Terminal$.$anonfun$withOut(Terminal.scala:550)
    at scala.util.DynamicVariable.withValue(DynamicVariable.scala:62)
    at scala.Console$.withErr(Console.scala:196)
    at sbt.internal.util.Terminal$.withOut(Terminal.scala:550)
    at sbt.internal.util.Terminal$.withStreams(Terminal.scala:350)
    at sbt.xMain$.withStreams(Main.scala:87)
    at sbt.xMain$.run(Main.scala:121)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at sbt.internal.XMainConfiguration.run(XMainConfiguration.java:56)
    at sbt.xMain.run(Main.scala:46)
    at xsbt.boot.Launch$.$anonfun$run(Launch.scala:149)
    at xsbt.boot.Launch$.withContextLoader(Launch.scala:176)
    at xsbt.boot.Launch$.run(Launch.scala:149)
    at xsbt.boot.Launch$.$anonfun$apply(Launch.scala:44)
    at xsbt.boot.Launch$.launch(Launch.scala:159)
    at xsbt.boot.Launch$.apply(Launch.scala:44)
    at xsbt.boot.Launch$.apply(Launch.scala:21)
    at xsbt.boot.Boot$.runImpl(Boot.scala:78)
    at xsbt.boot.Boot$.run(Boot.scala:73)
    at xsbt.boot.Boot$.main(Boot.scala:21)
    at xsbt.boot.Boot.main(Boot.scala)
[error] java.lang.NoClassDefFoundError: Could not initialize class com.swoval.runtime.ShutdownHooks
[error] Use 'last' for the full log.
[warn] Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? (default: r)
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: Could not initialize class com.swoval.runtime.ShutdownHooks
        at com.swoval.runtime.ShutdownHooks.run(ShutdownHooks.java:25)

EDIT3:尝试创建一个新用户并下载 sbt,但仍然遇到同样的问题。将尝试完全删除 Java,因为它似乎是罪魁祸首并重新安装。

我假设原因 - 你使用错误的 Java JDK 用于 Mac OS 和 M1 芯片。您需要安装正确的 Java JDK 版本,我为此使用 sdkman。

  1. 安装sdk man:
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
  1. 设置 sdk man 不使用 rosetta2_compatbile 版本:
vim .sdkman/etc/config

并设置sdkman_rosetta2_compatbile=false

  1. 从列表中选择 java JDK:
sdk list java

================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 Azul Zulu     |     | 16.0.1       | zulu    |            | 16.0.1-zulu
               |     | 11.0.11      | zulu    |            | 11.0.11-zulu
               |     | 8.0.292      | zulu    |            | 8.0.292-zulu
 BellSoft      |     | 16.0.1       | librca  |            | 16.0.1-librca
               |     | 11.0.11      | librca  |            | 11.0.11-librca
               |     | 8.0.292      | librca  |            | 8.0.292-librca
 Java.net      |     | 18.ea.3      | open    |            | 18.ea.3-open
               |     | 18.ea.2      | open    |            | 18.ea.2-open
               |     | 18.ea.1      | open    |            | 18.ea.1-open
               |     | 17.ea.28     | open    |            | 17.ea.28-open
               |     | 17.ea.27     | open    |            | 17.ea.27-open
               |     | 17.ea.26     | open    |            | 17.ea.26-open
               |     | 17.ea.25     | open    |            | 17.ea.25-open
================================================================================

并使用命令 sdk install java IDENTIFIER 安装它,即:

sdk install java 16.0.1-zulu

与鲍里斯的建议类似,问题出在 Java 的安装版本上。

如果您使用 sdk-man 作为包管理器,AdoptOpenJDK 有两个版本,.j9 和 .hs,如下所示:

AdoptOpenJDK | >>> | 16.0.1.j9    | adpt    | installed  | 16.0.1.j9-adpt
             |     | 16.0.1.hs    | adpt    |            | 16.0.1.hs-adpt
             |     | 11.0.11.j9   | adpt    |            | 11.0.11.j9-adpt
             |     | 11.0.11.hs   | adpt    |            | 11.0.11.hs-adpt

出于某种原因,OpenJDK 版本 .hs-adpt 对每个版本都显示此问题,但 .j9-adpt 版本工作正常。

TLDR:安装您需要的 Java 的 .j9-adpt 版本,否则 .hs-adpt 不适用于 sbt version 1.5.4