我對Scala沒有經驗,所以這個問題可能是基本的。我嘗試從Java內部使用Scala類,基於本教程中的「Person」示例:http://www.codecommit.com/blog/java/interop-between-java-and-scalaNoClassDefFoundError使用來自Java的Scala類
我創建兩個源文件,一個Scala和一個Java,如下所示。
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();
}
}
我可以編譯(有一個警告,我不明白),但我得到一個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
任何想法我做錯了什麼?
原因的例外是[這裏](http://javarevisited.blogspot.in/2011/06/noclassdeffounderror-exception-in.html)請查看 – gks
難道你的類路徑中包含scala-library.jar? –
雖然嘗試同樣的例子,我得到了以下錯誤,請幫助.. 'C:\ Users \ user1 \ Desktop> java -cp。:scala-library-2.11.8.jar測試 錯誤:找不到或加載主類測試' – rinuthomaz