swing的NoClassDefFoundError

swing的NoClassDefFoundError

本文介绍了带有sbt和scala.swing的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我是JVM领域的新手,如果这是一个常见问题,我深表歉意。我正在OSX上使用Scala(2.12)和sbt 0.13.13。

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.

我正在开发一个依赖GUI库的小型应用程序 scala.swing (2.10.x)。我几乎立即遇到示例代码()。

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).

具体来说,当调用 sbt run 时,我会得到一个堆栈跟踪,其开头为:

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 在scala stdlib中似乎是一个类/特征)

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

阅读SO和其他内容表明当无法在运行时随后找到在编译时出现的代码时,通常会发出此类异常。确实,代码可以很好地编译,只是在运行代码时才会出现问题。

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.

我发现的所有建议都是调和您的 classpath 解决这些问题。但是,如果相信 sbt 控制台,则我的编译时和运行时类路径是相同的:

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

因此,我发现自己处于法医僵局之中。任何关于下一步寻找的建议将不胜感激。为了清楚起见,到目前为止,这仅在 scala.swing 中发生。我在Scala还有另外两个小项目,没有任何问题。令人困惑的是,缺失类似乎是scala标准的lib材料。

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.

推荐答案

NoClassDefFoundError 指出了一个问题,其中您混合了为不同的主要Scala版本编译的库。如果使用Scala 2.12,则还必须使用具有匹配版本的Swing模块。在Scala 2.11之前,Swing的发布是这样的:

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

然后将其移至 org.scala- lang.modules 组。您的生成文件应包含以下内容:

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"

(似乎最新的Scala 2.11兼容版本 1.0.2 尚未针对Scala 2.12发布,因此您需要直接跳至 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).

这篇关于带有sbt和scala.swing的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:55