2

I am trying to do a basic scala HelloWorld in Eclipse 2019 and I am getting an error.

The following is my code and the error it is producing. Can someone please help me address this error in eclipse? Thanks

package hello

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$ at HelloWorld/hello.HelloWorld$.main(HelloWorld.scala:5) at HelloWorld/hello.HelloWorld.main(HelloWorld.scala) Caused by: java.lang.ClassNotFoundException: scala.Predef$ at

java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 2 more

Scala library is already in source path

Phantom
  • 23
  • 1
  • 8

2 Answers2

2

There are two required places to set your class-path:

  1. Build/compilation time class-path: (i) Right-click on your project, (ii) Buildpath > Configure BuildPath, (iii) Add Library (or jar), (iv) Select the Scala Library. This one you already have as supported by your screenshot.

  2. Run-time compilation class-path: This needs to be explicitly set in the Run-time configuration to also include the scala library: (i) Run configurations..., (ii) Classpath, (iii) Add Jar and use the scala-library jar. For this option, I have not tested whether User vs Bootstrap matters. Furthermore, I was unable to use the Add Library here, only Add jar results in a functioning run within Eclipse.

The second option is the likely cause of the error you are getting.

1

You need to add Scala library to your classpath.

From Eclipse:

  1. Right-click on your project
  2. Configure Buildpath
  3. Add Library
  4. Select the Scala Library
Gal Naor
  • 2,397
  • 14
  • 17
  • My scala library is already in the path. And it is still giving me the same error. – Phantom Apr 11 '19 at 20:18
  • Are you sure you didn't have missing scala-library.jar on your runtime classpath? maybe you don't have scala-library dependency in your maven/sbt/gradle? – Gal Naor Apr 11 '19 at 20:27
  • Could you specify how to check if scala-library.jar is on my runtime classpath. Thanks – Phantom Apr 11 '19 at 20:35
  • at sbt shell, type `show runtime:fullClasspath`. then you will see list of many jar. `scala-library.jar` should be there - something like: `[info] * Attributed(/Users/.../.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.7.jar)` – Gal Naor Apr 11 '19 at 20:39
  • do I need to install sbt to do scala in eclipse? I haven't done it. Do you think that is why scala is not running on eclipse? – Phantom Apr 11 '19 at 20:44
  • no, another way, in eclipse, to see the runtime classpath is to go to Run -> Run Configurations... -> Classpath. look at the jars there. – Gal Naor Apr 11 '19 at 20:46
  • There I only see Hello(project name) and under it JRE System Library[JavaSE-12]. How do I add the Scala library here? Thanks – Phantom Apr 11 '19 at 20:51
  • Download scala libs from here - https://downloads.lightbend.com/scala/2.12.8/scala-2.12.8.zip and import the jar by using this answer - https://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse – Gal Naor Apr 11 '19 at 20:59