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

梦想与她 提交于 2019-12-21 07:10:11

问题


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$1.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

回答1:


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

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

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




回答2:


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:

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

Check the Scala version installed:

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

Check the build.sbt to have the right version of Scala:

scalaVersion := "2.11.8"


来源:https://stackoverflow.com/questions/14417814/why-do-i-get-java-lang-noclassdeffounderror-when-i-trying-to-run-this-code

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