17

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:

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)

In addition, when I change last line to:

case x :: xs => 1 :: removeLast(xs)}

then it works.

What might the problem be?

Dawid Mazuruk
  • 233
  • 1
  • 3
  • 9
  • You can change signature to `def removeLast[T](list:List[T]):List[T]` – Andrzej Jozwik Oct 15 '14 at 20:04
  • Thanks for the answer, unfortunately I've already tried it and the result is as described above. – Dawid Mazuruk Oct 15 '14 at 20:13
  • Idea use own scala compiler. So you should create ticket to https://youtrack.jetbrains.com/issues/SCL . Check first if you have the latest version of the plugin 135.1228 – Andrzej Jozwik Oct 16 '14 at 06:16
  • Thank you for the link, it turned out it is already reported problem (https://youtrack.jetbrains.com/issue/SCL-7691). So I have switched to Intellij IDEA 13.0.4 and now everything works. – Dawid Mazuruk Oct 16 '14 at 18:40
  • FYI, I use IntelliJ 13.1.5 and encountered the same problem. Fixed it thanks to Tomek's anwser. – Matthieu Rouget Oct 27 '14 at 10:20

11 Answers11

29

I had this issue. Agree with Andrzej, idea uses its own compiler, so you have to disable it somehow. Go to Settings->Scala->Worksheet and uncheck "Run worksheet in the compiler process".

Tomek Kozlowski
  • 604
  • 7
  • 6
  • 2
    Thanks. Worked like a charm. This is the "Scala" under Project Settings. It helps to uncheck as you said under "Project Defaults". – Arjun Guha Oct 23 '14 at 18:23
  • Unchecking that option caused: 1) in the worksheet, the line (on the result tab) which reported "java.lang.NoSuchMethodError..." suddenly disappeared, but still didnt evaluate 2) my Idea to created lots of java.exe and conhost.exe processes, that took 100% cpu and made Idea incredibly slow and un-responsive. Is there any other solution? – PaoloC Oct 29 '14 at 21:46
  • 2
    For intelij 2017, I followed this step `File|Other Settings|Default Preferences|Languages & Frameworks| scala|Worksheet` – Miae Kim Mar 30 '18 at 20:06
7

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

  <dependency>
        <groupId>org.scalatest</groupId>
        <artifactId>scalatest_2.11</artifactId>
         <version>2.2.4</version>
         <scope>test</scope>
  </dependency>

helped

viniolli
  • 71
  • 1
  • 1
  • In general, make sure that you use the same version of scala for dependent libraries. Had the same problem with scalatest. – vks2106 Mar 05 '20 at 11:14
6

So, although the above didn't solve my problem it is related to intellij.

Basically, it was preferring the Scala SDK to resolve the Class::method instead of loading from the dependencies.

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).

I literally just deleted the Scala SDK from my project settings and 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.

Anytime you see "NoSuchMethodError" it always means there is a version conflict; it's a question of why.

Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
  • In my case even deleting it from project settings wasn't enough, I had to went to project .iml file and delete the incorrect scala version manually – Arthur Kalimullin Jan 25 '19 at 15:35
2

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.

<!-- 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

<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>
Franzi
  • 1,791
  • 23
  • 21
2

Error java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object

Reason This 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.

Resolution This 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.

Suggestion Request you to set the below versions before doing any coding: spark - 2.2.0 scala - 2.11.6

Also I used the below 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>
1

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.

So, it seems this is a compatibility issue, so I would check dependencies in the future.

Roberto
  • 987
  • 1
  • 9
  • 21
1

I solved this by set my scala sdk version in my project from 2.12 to 2.11.

Coo1 min
  • 21
  • 1
1

it is version problem, just use scala sdk version to 2.11.

刘孟德
  • 171
  • 1
  • 8
0

I have the same problem. When you change it to using map function it works! I don't know why but thats how to fix it.

M.Rez
  • 1,802
  • 2
  • 21
  • 30
0

I have found that this can be caused by having differing versions of scalatest and scalamock. The Following 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>
Andrew Long
  • 863
  • 4
  • 9
0

I had the same thing when adding json4. i solved it by changing the artifactId from json4s-native_2.12 to - 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 properties xml node in the pom.xml file, mine is : <scala.version>2.11</scala.version>.)

Ohad Bitton
  • 460
  • 2
  • 14