当我在 ScalaIDE 中运行我的代码时,为什么会出现 `java.lang.NoClassDefFoundError: scala/Function1`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24415698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:04:52  来源:igfitidea点击:

Why do I get `java.lang.NoClassDefFoundError: scala/Function1` when I run my code in ScalaIDE?

javascalamavennoclassdeffounderrorscala-ide

提问by blue-sky

Here is a simple test I'm using to invoke a Scala method from Java:

这是我用来从 Java 调用 Scala 方法的一个简单测试:

public static void main(String args[]) {
  java.util.Map<String, java.util.List<String>> rec = news.recommend.DriverObj.runTest();     
  System.out.println(rec.toString());
}

Here is the definition of the Scala method:

这是 Scala 方法的定义:

def runTest: java.util.Map[String, java.util.List[String]] = {
  new java.util.HashMap[String, java.util.List[String]]
}

But it throws an error:

但它抛出一个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function1
    at news.recommend.DriverObj.runTest(DriverObj.scala)

What should I do to make this running smoothly?

我应该怎么做才能使这个运行顺利?

Update : I'm running it via Eclipse and my build path contains :

更新:我通过 Eclipse 运行它,我的构建路径包含:

enter image description here

在此处输入图片说明

So Scala library should be found ?

那么应该找到 Scala 库吗?

采纳答案by Riduidel

adding the Scala dependency to the maven build can fix the issue :

将 Scala 依赖项添加到 Maven 构建可以解决此问题:

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.10.3</version>
</dependency>

回答by Kuang Wenyi

As for me, in the configuration of "Run/Debug configurations" [idealJ]

至于我,在“运行/调试配置”的配置中[idealJ]

remember to choose the "Include dependencies with "Provided" scope"

请记住选择“包含具有“提供”范围的依赖项”