NoClassDefFoundError: scala/ScalaObject in mixed Scala/Java project
Asked Answered
H

1

6

Background: I am a C# developer, but my next project needs to be on the JVM. I have researched Scala and I think that it lets me to what I can in C# (Lambdas, inferred types, functional-style, etc). I would classify myself as a very novice Java developer... especially when it comes to using the IDE's, etc.

I have a Scala project that seems to work... I made a Java project and referenced the Scala project The editor seems to know about the classes in the Scala project (typeahead, etc)

When I try to run the Java project, I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at learning_scala.java.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

Clearly something has gone awry in the Java/Scala compilation, but I have no idea how to resolve it (my lack of Java experience being the primary culprit here)

I'm using Eclipse w/ the official Scala plugin... all of which are up to date as of 2-3 days ago.

Thanks

Heraclitus answered 16/10, 2011 at 0:15 Comment(2)
This means that the Scala library (scala-library.jar) is not being loaded by Eclipse. I don't know how to fix this in Eclipse; on the command-line, you add the jar to the classpath: -cp /path/to/scala/lib/scala-library.jar. Hopefully an Eclipse user can help. P.S. Scala can do quite a bit more than C# when it comes to lambdas, inferred types, etc.--I hope it's not too hard to go back!Constituency
Scala is the first JVM based language that I have been remotely interested in learning. Part of this is also forcing myself to learn something new... C# and C++ are very familiar to me after 10 years and I need to challenge myself to do something new.Heraclitus
K
8

You need to add the scala-library.jar library to your classpath. You can do that a few different ways but the simplest is to copy the file into your project (drag and drop it into your eclipse package explorer). Next, right click on the file and select add to build path. That adds the scala jar to your classpath. That should fix the problem.

Kofu answered 16/10, 2011 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.