NoClassDefFoundError with sbt and scala.swing

你说的曾经没有我的故事 提交于 2020-01-02 04:54:05

问题


I'm new to JVM land so I apologize if this is a common problem. I'm using Scala (2.12) with sbt 0.13.13 on OSX.

I'm working on a tiny app that depends on the GUI library scala.swing (2.10.x). I ran into a runtime issue almost immediately with example code (http://otfried.org/scala/index_28.html).

Specifically, when invoking sbt run I get a stacktrace leading with:

[error] (run-main-0) java.lang.NoClassDefFoundError: scala/Proxy$class
java.lang.NoClassDefFoundError: scala/Proxy$class
    at scala.swing.Window.<init>(Window.scala:25)
    at scala.swing.Frame.<init>(RichWindow.scala:75)
    at scala.swing.MainFrame.<init>(MainFrame.scala:19)

(Proxy appears to be a class/trait in the scala stdlib)

Reading on SO and elsewhere suggests this kind of exception is typically emitted when code present at compile time cannot be located subsequently at runtime. Indeed, the code compiles just fine, it is only when running the code that the problem occurs.

All suggestions I've found are to reconcile your classpath to resolve these issues. However, if the sbt console is to believed, my compile-time and run-time classpaths are identical:

> show compile:fullClasspath
[info] * Attributed(/Users/chris/Projects/thing2/target/scala-2.12/classes)
[info] * Attributed(/Users/chris/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.1.jar)
[info] * Attributed(/Users/chris/.ivy2/cache/org.scala-lang/scala-swing/jars/scala-swing-2.10.6.jar)
[success] Total time: 0 s, completed Dec 24, 2016 7:01:15 PM
> show runtime:fullClasspath
[info] * Attributed(/Users/chris/Projects/thing2/target/scala-2.12/classes)
[info] * Attributed(/Users/chris/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.1.jar)
[info] * Attributed(/Users/chris/.ivy2/cache/org.scala-lang/scala-swing/jars/scala-swing-2.10.6.jar)
[success] Total time: 0 s, completed Dec 24, 2016 7:01:19 PM

So, I find myself at a bit of a forensic impasse. Any suggestions on where to look next would be much appreciated. For clarity, this has only happened with scala.swing thus far. I have a couple other small projects in Scala that haven't had any issues. What's perplexing is the "missing" class seems to be scala standard lib material.


回答1:


NoClassDefFoundError points to a problem where you mix libraries that were compiled for different major Scala versions. If you use Scala 2.12, you must also use the Swing module with a matching version. Before Scala 2.11, Swing has been published with an artifact like this:

"org.scala-lang" % "scala-swing" % scalaVersion.value

It was then moved to the org.scala-lang.modules group. Your build file should contain something like this:

scalaVersion := "2.12.1"

libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "2.0.0-M2"

(it seems the latest Scala 2.11 compatible version "1.0.2" has not been published for Scala 2.12, and so you need to jump straight to "2.0.0-M2" which should be mostly source compatible).



来源:https://stackoverflow.com/questions/41318393/noclassdeffounderror-with-sbt-and-scala-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!