Intellij IDEA java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26390219/
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
Intellij IDEA java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object
提问by Dawid Mazuruk
I have a following function:
我有以下功能:
def removeLast(list: List[Int]): List[Int] = list match {
case List() => List()
case List(x) => List()
case x :: xs => x :: removeLast(xs)
}
When I define it and use it from the sbt console everything works just fine. But when I create a worksheet in Intellij IDEA and try to run it then the following exception appears:
当我定义它并从 sbt 控制台使用它时,一切正常。但是当我在 Intellij IDEA 中创建一个工作表并尝试运行它时,会出现以下异常:
java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object; at week5.A$A26$A$A26.removeLast(lists.sc8362409100671270508.tmp:30) at #worksheet#.#worksheet#(lists.sc8362409100671270508.tmp:33)
java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object; 在 week5.A$A26$A$A26.removeLast(lists.sc8362409100671270508.tmp:30) 在 #worksheet#.#worksheet#(lists.sc8362409100671270508.tmp:33)
In addition, when I change last line to:
此外,当我将最后一行更改为:
case x :: xs => 1 :: removeLast(xs)}
then it works.
那么它的工作原理。
What might the problem be?
可能是什么问题?
回答by Tomek Kozlowski
I had this issue. Agree with Andrzej, idea uses its own compiler, so you have to disable it somehow. Go to Settings->Scala->Worksheetand uncheck "Run worksheet in the compiler process".
我有这个问题。同意 Andrzej,idea 使用自己的编译器,所以你必须以某种方式禁用它。转到Settings->Scala->Worksheet并取消选中“在编译器进程中运行工作表”。
回答by viniolli
Any answer wasn't usefull in my case. Still i found a solution which worked for me.. It was problem with scalatest version. In pom.xml uprade to
在我的情况下,任何答案都没有用。我仍然找到了一个对我有用的解决方案。这是 Scalatest 版本的问题。在 pom.xml 升级到
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
helped
有帮助
回答by Christian Bongiorno
So, although the above didn't solve my problem it is related to intellij.
所以,虽然上面没有解决我的问题,但它与 intellij 相关。
Basically, it was preferring the Scala SDK to resolve the Class::method instead of loading from the dependencies.
基本上,它更喜欢 Scala SDK 来解析 Class::method 而不是从依赖项加载。
I used
我用了
-verbose:class
-详细:类
in the JVM switch to have it show me where it was looking; immediately cluing me into it being because it's trying to load the class from the Scala SDK (it would expect it to pull in libs from Maven).
在 JVM 开关中让它向我展示它正在寻找的位置;立即将我引入它,因为它试图从 Scala SDK 加载类(它希望它从 Maven 中获取库)。
I literally just deleted the Scala SDK from my project settingsand the problem went away. So far, my experience with Scala (and definitely in a mixed Java environment) leads me to believe it has a ways to go to mature. This is such a fundamental class/method I can't believe it vanished between versions. The scala version I had installed was 2.11. Apparently what get's pulled in is 2.10.4 from maven.
我真的只是从我的项目设置中删除了 Scala SDK,问题就消失了。到目前为止,我在 Scala 方面的经验(绝对是在混合 Java 环境中)让我相信它有走向成熟的道路。这是一个如此基本的类/方法,我不敢相信它在版本之间消失了。我安装的 Scala 版本是 2.11。显然,从 maven 中提取的是 2.10.4。
Anytime you see "NoSuchMethodError" it alwaysmeans there is a version conflict; it's a question of why.
每当您看到“NoSuchMethodError”时,它总是意味着存在版本冲突;这是一个为什么的问题。
回答by Franzi
Like others said here, I was having the same problem due I had some libraries using 2.10 in spite of having scalatest at 2.11.
就像这里的其他人所说的那样,我遇到了同样的问题,因为我有一些库使用 2.10,尽管在 2.11 上有 scalatest。
<!-- http://www.scalactic.org/ -->
<dependency>
<groupId>org.scalactic</groupId>
<artifactId>scalactic_2.11</artifactId>
<version>${scalactic.version}</version>
<scope>test</scope>
</dependency>
<!-- http://www.scalatest.org/ -->
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>${scalactic.version}</version>
<scope>test</scope>
</dependency>
Chech that all libraries that you are using are in same Scala version
检查您使用的所有库都在同一个 Scala 版本中
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
To
到
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
Having as properties
具有作为属性
<properties>
<scala.tools.version>2.11.8</scala.tools.version>
<scala.version>2.11.8</scala.version>
<scalactic.version>3.0.0</scalactic.version>
<!-- Library Versions -->
<spark.version>2.0.0</spark.version>
....
</properties>
回答by Aakash Agrawal
Errorjava.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object
错误java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object
ReasonThis error is specifically due to version mismatch between spark and scala. I faced the error while I was using spark 2.2.0 and scala 2.10.6. Then I changed to different scala versions but I got no success.
原因此错误特别是由于 spark 和 scala 之间的版本不匹配。我在使用 spark 2.2.0 和 scala 2.10.6 时遇到了错误。然后我换了不同的scala版本,但没有成功。
ResolutionThis error is resolved only when I changed the scala version to 2.11.6 . This version was a perfect match for spark 2.2.0. May be you can try higher versions of scala for the same issue , but I tried for 2.12.x but didnt work.
解决方法 仅当我将 scala 版本更改为 2.11.6 时才解决此错误。此版本与 spark 2.2.0 完美匹配。也许您可以针对同一问题尝试更高版本的 scala,但我尝试了 2.12.x 但没有奏效。
SuggestionRequest you to set the below versions before doing any coding: spark - 2.2.0 scala - 2.11.6
建议要求您在进行任何编码之前设置以下版本:spark - 2.2.0 scala - 2.11.6
Also I used the below pom :
我也使用了下面的 pom :
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
回答by Roberto
I just encountered the same problem. Turned out that I had downloaded the wrong version of Akka which included scala-library-2.10.x, while my project uses 2.11.6. Grabbing the latest version of Akka, which includes 2.11.5, solved the problem.
我刚刚遇到了同样的问题。结果是我下载了错误的 Akka 版本,其中包括 scala-library-2.10.x,而我的项目使用的是 2.11.6。抓取最新版本的 Akka,包括 2.11.5,解决了这个问题。
So, it seems this is a compatibility issue, so I would check dependencies in the future.
所以,这似乎是一个兼容性问题,所以我将来会检查依赖项。
回答by Coo1 min
I solved this by set my scala sdk version in my project from 2.12 to 2.11.
我通过将我的项目中的 scala sdk 版本从 2.12 设置为 2.11 解决了这个问题。
回答by u9411432
it is version problem, just use scala sdk version to 2.11.
这是版本问题,只需使用 scala sdk 版本到 2.11。
回答by Andrew Long
I have found that this can be caused by having differing versions of scalatest and scalamock. The Following Maven
我发现这可能是由不同版本的 scalatest 和 scalamock 引起的。下面的Maven
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId><!-- this was previously 2.10 -->
<version>2.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalamock</groupId>
<artifactId>scalamock-scalatest-support_2.11</artifactId>
<version>3.2</version>
<scope>test</scope>
</dependency>
回答by Ohad Bitton
I had the same thing when adding json4. i solved it by changing the artifactId from json4s-native_2.12to - json4s-native_2.11.
I guess this is related to the scala version you are using mine was 2.11 and not 2.12 (you can see yours in the propertiesxml node in the pom.xmlfile, mine is : <scala.version>2.11</scala.version>.)
添加json4. 我通过将 artifactId 从更改json4s-native_2.12为 -来解决它json4s-native_2.11。我想这与您使用的 scala 版本有关,我的版本是 2.11 而不是 2.12(您可以properties在pom.xml文件的xml 节点中看到您的版本,我的是 : <scala.version>2.11</scala.version>.)

