Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

noClassDefFoundError using Scala Plugin for Eclipse

I successfully implemented and ran several Scala tutorials in Eclipse using the Scala plugin. Then suddenly I tried to compile and run an example, and this error came up:

Exception in thread "main" java.lang.NoClassDefFoundError: hello/HelloWorld
Caused by: java.lang.ClassNotFoundException: hello.HelloWorld
 at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

After this point I could no longer run any Scala programs in Eclipse. I tried cleaning and rebuilding my project, closing and reopening my project, and closing and reopening Eclipse.

Eclipse version number 3.5.2 and Scala plugin 2.8.0

Here is the original code:

package hello

object HelloWorld {
 def main(args: Array[String]){
  println("hello world")
 }
}
like image 845
Jacob Lyles Avatar asked Mar 29 '10 01:03

Jacob Lyles


4 Answers

If you see this when you attempt to run as a Scala application then the most likely explanation is that your project didn't compile and no class files were generated. Please check whether or not that's the case: look in your project's output folder for hello/HelloWorld.class.

If your project didn't compile that could either be because there's an error which you've missed (and if this error isn't being reported in the Problems view that could be a bug, in which case please open a ticket on Trac) or because you've turned off automatic builds and not done a manual build of your project.

like image 116
Miles Sabin Avatar answered Oct 12 '22 01:10

Miles Sabin


I had the same problem. Project doesn't compile but there are no errors highlighted and AFAIK the code is OK. It seems to be a problem with the Run Configurations.

Solution 1: Delete the existing Run Configuration for your object and create a new one

Solution 2: Create a new object and cut / paste all your code into that file

like image 22
Luigi Plinge Avatar answered Oct 12 '22 01:10

Luigi Plinge


When running "clean" does not un-hose Eclipse, I next try saving my work, exiting Eclipse, and re-starting. That usually gets things going again, but not always. A few times I've had to update the Scala plugin with a more recent version (I'm using the latest nightly), to get things working again. I doubt that this worked because the new plugin happened to fix the bug, but rather expect that loading the new plugin gives the whole Eclipse-Scala system a "total reset" that gets it unhosed.

like image 26
Terry Woodrff Avatar answered Oct 12 '22 01:10

Terry Woodrff


I was getting this problem in a project that combined .java & .scala files. The solution for me was:

  1. Remove all .java files
  2. Edit the scala code as needed so it compiles without them.
  3. Add the .java files back in.
  4. Edit the scala code back.

The other solutions given here didn't work for me. I tried: clean project, restarting Eclipse, closing-&-opening the project, creating a new .scala file. No joy.

I'm using Eclipse 3.7 (latest stable), Scala IDE 2.0.0 and Scala 2.9 on Ubuntu Linux 11.10.

The symptoms in my case were:

  • My project was working, but then it stopped compiling for no apparent reason. The IDE didn't show any compilation errors for .scala files, but there were no .class files in the output directory & I got a NoClassDefError if I tried to run anything.
  • If I created a deliberate error in a .scala file, that did get picked up as a compilation error.
  • The .java files were registering errors due to the missing scala classes.

I suppose there's probably a boot-strapping bug somewhere in the IDE plugin for .java/.scala mixes. I've done hybrid projects with this setup without problems, so it's only triggered in some situations. I don't know what the trigger is, but once triggered, there's no nice solution.

like image 1
Daniel Winterstein Avatar answered Oct 12 '22 02:10

Daniel Winterstein