scala 当我尝试运行此代码时,为什么会出现 java.lang.NoClassDefFoundError?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14417814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:51:26  来源:igfitidea点击:

Why do I get java.lang.NoClassDefFoundError when I trying to run this code?

scalaclassnotfoundexception

提问by mcandre

I want to map over the characters in a string, but I'm getting runtime errors.

我想映射字符串中的字符,但出现运行时错误。

Example:

例子:

object Hello {
    def hello(c: Char) {
        print(c)
    }

    def main(args: Array[String]) {
        "Hello World!".map(hello)
    }
}

Trace:

痕迹:

scalac Hello.scala
java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: scala/LowPriorityImplicits
    at Hello.main(Hello.scala)
Caused by: java.lang.ClassNotFoundException: scala.LowPriorityImplicits
    at java.net.URLClassLoader.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 1 more
make: *** [test] Error 1

回答by fhuertas

I think that your problem is that scala library is not in your runtime classpath. you must manually add manually.

我认为您的问题是 Scala 库不在您的运行时类路径中。您必须手动手动添加。

If you are using tools like maven or sbt, maybe the dependency is marked as provided instead compiled.

如果您使用的是 maven 或 sbt 之类的工具,可能依赖项被标记为提供而不是编译。

If you are not using these tools, add "scala-library.jar" to your library directory

如果您不使用这些工具,请将“scala-library.jar”添加到您的库目录中

回答by Yuchen Zhong

Also seeing this problem because I don't have the right version of Scala. For those who are using IntelliJ, you can add/change the scala SDK under File> Project Structures> Global Libraries:

也看到这个问题,因为我没有正确版本的 Scala。对于使用 IntelliJ 的用户,您可以在File> Project Structures> Global Libraries下添加/更改 scala SDK :

enter image description here

在此处输入图片说明

If you are compiling and running your project in command line, make sure you have the right version of Scala installed too. e.g.:

如果您在命令行中编译和运行您的项目,请确保您也安装了正确版本的 Scala。例如:

Check the Scala version installed:

检查安装的 Scala 版本:

$ scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL

Check the build.sbtto have the right version of Scala:

检查build.sbt以获得正确版本的 Scala:

scalaVersion := "2.11.8"