Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError when running Scala jar file

I have a small application with RemoteActors, and I want to make a jar file from it. When I try to execute it it gets this exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:616)
  at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: scala/actors/Combinators
  at pingpong.PingApp$.main(PingApp.scala:5)
  at pingpong.PingApp.main(PingApp.scala)
  at pingpong.ScalaEntryPoint.main(ScalaEntryPoint.java:5)
  ... 5 more
Caused by: java.lang.ClassNotFoundException: scala.actors.Combinators
  at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
  ... 8 more

I have included scala-library.jar in my jar file, and it is in the classpath. Otherwise java would stop at finding the class ScalaObject.

like image 266
laci37 Avatar asked Dec 15 '11 21:12

laci37


2 Answers

Java doesn't handle "jar within a jar". The default classloader can't load classes that way.

From your stacktrace, it looks like you've used eclipse's export->java->runnable jar. It is supposed to include a special classloader which handles jar in jar loading but it seems not to be working.

I would guess that something went wrong building the jar or it is confused by scala being both included in your jar and your classpath. You could try this again using "extract libraries into jar" rather than "package libraries into jar". You could try it without scala on the classpath.

Failing that there are other options if you want a single shippable jar:

  • You can unpack all of the jars you depend on and repack them with your compiled classes (should be what "extract" does above). Maven's shade plugin can help with this.

  • You can use a different solution that will provide a classloader implementation that can load nested jars, like OneJar

Hope this is useful, if you need more help you need to describe exactly what steps you take to produce this jar, and probably the output of "jar tvf myjar.jar" will help too.

like image 63
Brian Smith Avatar answered Oct 05 '22 16:10

Brian Smith


The problem was a version mismatch between the Scala plugin on Eclipse and the installed Scala version. actors/Combinators.scala appeared in 2.8, and the Eclipse plugin was 2.8 however I included 2.7.7 scala-library.jar

like image 42
laci37 Avatar answered Oct 05 '22 14:10

laci37