运行 Scala jar 文件时出现 NoClassDefFoundError
我有一个带有 RemoteActors 的小应用程序,我想用它制作一个 jar 文件。当我尝试执行它时,它会出现此异常:
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
我已将 scala-library.jar 包含在我的 jar 文件中,并且它位于类路径中。否则,java 将在查找类 ScalaObject 时停止。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 不处理“罐子里的罐子”。默认类加载器无法以这种方式加载类。
从你的堆栈跟踪来看,你似乎已经使用了 Eclipse 的导出->java->可运行 jar。它应该包括一个特殊的类加载器,用于处理 jar 中的 jar 加载,但它似乎不起作用。
我猜想构建 jar 时出了问题,或者它因 scala 同时包含在你的 jar 和类路径中而感到困惑。您可以使用“将库提取到 jar 中”而不是“将库打包到 jar 中”再次尝试此操作。你可以尝试在类路径上不使用 scala。
如果您想要一个可交付的 jar,那么还有其他选择:
您可以解压您依赖的所有 jar,并使用编译的类重新打包它们(应该是上面的“extract”所做的)。 Maven 的 shade 插件可以帮助解决此问题。
您可以使用不同的解决方案来提供可以加载嵌套 jar 的类加载器实现,例如 OneJar
希望这对您有用,如果您需要更多帮助,您需要准确描述生成此 jar 所采取的步骤,并且“jar tvf myjar.jar”的输出可能也会有所帮助。
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.
问题是 Eclipse 上的 Scala 插件与已安装的 Scala 版本之间的版本不匹配。 actor/Combinators.scala 出现在 2.8 中,Eclipse 插件是 2.8,但我包含了 2.7.7 scala-library.jar
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