Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala-library imported twice?

I'm just trying to execute a simple Hello World application in Scala on Intellij with SBT but even this... it seem to be way too tough.

I followed the instructions here : https://www.cloudera.com/tutorials/setting-up-a-spark-development-environment-with-scala/.html

I use to work with Eclipse and Maven... Here I'm on Intellij and SBT so I try my best but I'm a bit lost. Here is what I've done :

  • I installed Intellij, SBT.
  • I configured SBT launcher in Intellij by setting File > Settings > Build, Execution, Deployment > Build tools > sbt > Launcher
  • Create new Project with Java 1.8, SBT 1.3, Scala 2.13
  • At this step I couldn't create Scala class, but I noted that if i "Reimport sbt project" then the scala class will appear in the list of new element to create.
  • Create an object with main method :
    object HelloScala {
      def main(args: Array[String]): Unit = {
        println("Hello world!")
      }
    }

When I try to execute that main it result in the following error :

Error:scalac: Multiple 'scala-library*.jar' files (scala-library-2.13.0.jar, scala-library-2.13.0.jar) in Scala compiler classpath in Scala SDK sbt: org.scala-lang:scala-library:2.13.0:jar

My build.sbt :

name := "HelloScala"
version := "0.1"
scalaVersion := "2.13.0"

I checked the Compiler classpath in File > Project Structure > Libraries > Compiler classpath :

file:///AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.jar
file:///AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.0/scala-compiler-2.13.0.jar
file:///AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.0/scala-library-2.13.0.jar
file:///AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.13.0/scala-reflect-2.13.0.jar

I tried to delete target directory, invalidate cache, regenerate the .idea folder by deleting it and let intellij recreate it, I searched from other scala-library.jar in the project structure. I've found nothing...

Really... I don't understand why the scala-library is referenced twice.

If I change the scala version from 2.13.0 to 2.11.0, same problem :

Error:scalac: Multiple 'scala-library*.jar' files (scala-library-2.11.0.jar, scala-library-2.11.0.jar) in Scala compiler classpath in Scala SDK sbt: org.scala-lang:scala-library:2.11.0:jar

BUT ! If I comment the scala version in the build.sbt :

name := "HelloScala"
version := "0.1"
//scalaVersion := "2.11.0"

Then if I clear the target directory, reimport sbt project and try to execute, it works! And in the target directory, I've a new scala-2.12 folder... Where the hell it come from?!

For information, I'm on Windows 10, Intellij 2019.2, JDK 1.8, SBT 13

Any help is really appreciated!

like image 253
cenote Avatar asked Sep 07 '19 01:09

cenote


2 Answers

Try this:

External Libraries > [Right Click] sbt: org.scala-lang:scala-library:x.x.x:jar > Open Library Settings > Project Settings > Libraries

There's a section with Classes, Sources, & JavaDocs. Click JavaDocs & remove it with the minus sign above. Click OK.

Reimport your sbt project (on the sbt toolbar on the right).


Still not sure why the error occurs or why this fixes it.

like image 115
Jeffmylife Avatar answered Nov 04 '22 08:11

Jeffmylife


TL;DR

Revert your IDEA projects to sbt 1.2.8

Select sbt version 1.2.8 in the New Project dialog


Details

I've been fighting with the same issue this weekend with a fresh install of IntelliJ IDEA. I tried different versions of the IDE, and some of the other fixes suggested above, but the only solution that really seemed to work was manually editing the auto-generated .idea/libraries/sbt__org_scala_lang_scala_library_2_13_0_jar.xml file to remove the duplicate JAR entries.

I finally made the connection between the error and the new sbt version when I noticed this dialog in my shell:

[info] Welcome to sbt 1.3.0.
[info] Here are some highlights of this release:
[info]   - Coursier: new default library management using https://get-coursier.io
[info]   - Super shell: displays actively running tasks
[info]   - Turbo mode: makes `test` and `run` faster in interactive sessions. Try it by running `set ThisBuild / turbo := true`.
[info] See https://www.lightbend.com/blog/sbt-1.3.0-release for full release notes.

Since sbt 1.3.0 introduced Coursier, and the duplicate JAR entries in the IDEA-generated XML files were all stored in a Coursier cache directory, I thought reverting to sbt 1.2.8 might fix the problem.

Sure enough, I only get the "Multiple 'scala-library*.jar' files" error when using sbt 1.3.0. I re-created my project multiple times and have not seen the error when using sbt 1.2.8.

like image 5
DaoWen Avatar answered Nov 04 '22 08:11

DaoWen