NoClassDefFoundError uses Scala classes from Java
•
Java
I don't have Scala experience, so this problem may be basic According to the "person" example in this tutorial, I am using the scala class in Java: http://www.codecommit.com/blog/java/interop-between-java-and-scala
I create two source files, a Scala and a Java, as shown below
Person. scala:
class Person { def getName() = "Daniel Spiewak" }
Test. java:
public class Test { public static void main(String[] args) { Person p = new Person(); p.getName(); } }
I can compile (with a warning I don't understand), but when I try to run the program, I get a classnotfoundexception
$scalac Person.scala $javac Test.java ./Person.class: warning: Cannot find annotation method 'bytes()' in type 'ScalaSignature': class file for scala.reflect.ScalaSignature not found 1 warning $java Test Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:787) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.urlclassloader.defineClass(urlclassloader.java:447) at java.net.urlclassloader.access$100(urlclassloader.java:71) at java.net.urlclassloader$1.run(urlclassloader.java:361) at java.net.urlclassloader$1.run(urlclassloader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.urlclassloader.findClass(urlclassloader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at Test.main(Test.java:3) Caused by: java.lang.ClassNotFoundException: scala.ScalaObject at java.net.urlclassloader$1.run(urlclassloader.java:366) at java.net.urlclassloader$1.run(urlclassloader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.urlclassloader.findClass(urlclassloader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 13 more
Any idea what am I doing wrong?
Solution
You need to include the scala runtime library to use Scala classes This is the cause of compiler warnings and errors at run time
Try this
java -cp .:scala-library.jar Test
The name of the jar file may be different, but the point is to add it to your classpath
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码