尝试从Java实例化Scala类时出现异常

时间:2013-04-23 09:56:32

标签: java scala noclassdeffounderror

此问题不应该被关闭,因为建议的答案在这种情况下不回答问题。

以下代码会导致此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at scala.java.mixed.test.Driver.main(Driver.java:17)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 13 more

代码:

package parallel

import com.recommendations.EucleudianProperties

class TestData(var distanceMap : java.util.Map[java.lang.String , java.lang.String]) {

  object ProcessData{
    def apply {
      println("in apply method "+distanceMap.size)
    }
  }

}


package scala.java.mixed.test;

import java.util.HashMap;
import java.util.Map;
import parallel.TestData;

public class Driver {

    public static void main(String args[]){

        Map<String , String> distanceMap = new HashMap<String , String>();

        new TestData(distanceMap);
    }

}

导致此错误的原因是什么?我是否正确地从java类驱动程序调用Scala类TestData?

编辑:这是构建路径

enter image description here

1 个答案:

答案 0 :(得分:0)

我的问题是scala-library.jar没有在classpath中配置 Eclipse运行配置,一旦我添加了它,它就会按预期运行。这是我更新的类路径:

enter image description here