Multiple scala libraies causing error in intellij? - scala

I am using intellij 14 with scala 2.11.6 installed using home brew and symlink using
ln -s /usr/local/Cellar/scala/2.11.6/libexec/src /usr/local/Cellar/scala/2.11.6/src
ln -s /usr/local/Cellar/scala/2.11.6/libexec/lib /usr/local/Cellar/scala/2.11.6/lib
mkdir -p /usr/local/Cellar/scala/2.11.6/doc/scala-devel-docs
ln -s /usr/local/Cellar/scala/2.11.6/share/doc/scala /usr/local/Cellar/scala/2.11.6/doc/scala-devel-docs/api
I tried running a simple hello world but run into the following issue.
Error:scalac: Multiple 'scala-library*.jar' files (scala-library.jar, scala-library.jar, scala-library.jar) in Scala compiler classpath in Scala SDK scala-sdk-2.11.6
Edit:
So I check the compiler class path on global libraries and apparently there are multiple scal-library.jar
file:///usr/local/Cellar/scala/2.11.6/idea/lib/scala-library.jar
file:///usr/local/Cellar/scala/2.11.6/lib/scala-library.jar
file:///usr/local/Cellar/scala/2.11.6/libexec/lib/scala-library.jar
Does anyone know why?

Maybe you have used
/usr/local/Cellar/scala/2.11.6/
as the path for Scala SDK?
When you install scala with homebrew that path will contain not only the scala libraries, but also a symlink with the relevant libraries for intellij. So if you use the top-level install directory intellij will find the libraries twice.
Instead you should use
/usr/local/Cellar/scala/2.11.6/idea/lib

I had the same issue than you experimented and the solution, actually very easy, was actually erasing the .idea folder from the project, the problem is that the configuration inside of this folder (containing the set ups for example for the test, VCS, the runs, etc) gets corrupted with double entries (probably cos you update your Scala version), once you do this and reopen the project in Intellij the IDEA will generate a fresh new configuration for you.

This worked for me.
I'm using Idea 2019.2.2 and Windows 10.
In the folder .idea/libraries/ I had two files: sbt__org_scala_lang_scala_library_2_13_0_jar.xml and sbt__org_scala_lang_scala_library_2_13_0_jar2.xml.
I deleted the second file.
Then I opend the first one and there were duplicate lines:
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.0/scala-compiler-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.0/scala-compiler-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.0/scala-library-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.0/scala-library-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.13.0/scala-reflect-2.13.0.jar" />
So I deleted duplicates and errors disappeared.
Hope this will help someone else.

I also ran into that error. The fix I found was to remove the duplicate scala-library in the .iml file generated by intellij.
Basically I located the relevant .iml file by grepping the scala version, and found that two scala-library in that file. I removed the scala 2.11 version, and then it works.

Remove the following line in build.sbt:
...
scalaVersion := "2.13.0"
...
Try rebuilding and running it again

2019 update... I'm running Ubuntu Linux and IntelliJ community 2019.1 with sbt 2.13.1 and got the exact same error. I also found that if I built directly from sbt with "runMain package.MyClass" it worked, so I knew it must be an IntelliJ problem, not a "real" problem.
Anyway, I found the file .idea/libraries/sbt.. lots of crazy long name ... scala_library_2_13_1_jar.xml.
In it, there was an XML block , and in that block, two entries were duplicated:
I first noticed the scala-library one, and after deleting that, the error report came up about the scala-compiler duplicate too.
After deleting both duplicates, my build is now working.

To fix the problem go to project structure in intellij and go to global libraries
It should be like this
After that remove all the scala SDKs by hitting the - mark
Next, click the + and choose your Scala SDK version to add
After that please make sure to apply the changes and re-run the program

You have JAR files from multiple versions of scala-library.jar. In order to make the error go away you will have to delete the duplicates. To figure out which version you want to keep, you can view the manifest file inside each JAR:
META-INF/MANIFEST.MF
Inside the manifest file you should see something like this:
Manifest-Version: 1.0
Class-Path:
Implementation-Title: Scala-Library
Implementation-Version: 2.11.4
The error is happening because IntelliJ cannot figure out which version of a given Scala class to use.

Delete multiple versions of scala-library in sbt, leaving one.

Having similar symptoms, but on a Ubuntu machine, not using brew:
I am using /usr/share/sbt/bin/sbt-launch.jar as launcher. To fix the mentioned problem, I've had to purge the directories 1) project, 2) target and 3) .idea of the relevant Scala projects, doing sbt refresh in IntelliJ (View - Tool Windows -> sbt, hit the Reimport all sbt projects icon), and rebuild all modules afterwards.
As a last step, when the error occurs further, switch over to Ubuntu shell / terminal, and do a sbt clean compile in the problematic project folder. Fix the compile issues if some are occuring. If this does not help, change scalaVersion in build.sbt e.g. from 2.12.9 to 2.12.8 (the error occurs more often with 2.12.9), remove scalaOrganization definition (but keep organization). Repeat the sbt refresh of IntelliJ. OMG.

This worked for me:
In IDEA
Preferences -> Plugins -> Scala -> Update
Restart IDEA

Related

"object index is not a member of package views.html" when opening scala play project in scala ide

I've created a play project with play 2.3.7
In the project directory, I ran activator and ran the eclipse command to generate eclipse project files.
When I go to eclipse (I'm using the Scala IDE from typesafe Build id: 4.0.0-vfinal-20150119-1023-Typesafe , there is an error in my Application.scala file:
object index is not a member of package views.html
Is there something amiss with my setup? The app runs fine when I execute run at the activation console prompt.
Thanks!
EDIT: Added code
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
The error is on the 'Ok..' line.
There is a file in views called index.scala.html, and the app runs file when I run it from activator..
Occasionally after adding a view in Play 2.4.x, IntelliJ IDEA sometimes gets confused and absolutely refuses to build. Even rebuild Project fails:
This still happens from time-to-time in IDEA 15. And when it does, the command line provides the quickest, most-reliable fix:
sbt clean; sbt compile
That's it! IDEA will now compile the project as expected.
Update:
In the rare case that sbt compile completed successfully on the command line, but IntelliJ IDEA 15 still gives the same "object x is not a member" error, then this has solved IDEA's confusion:
File Menu:
The other solutions did not work for me. Some would give me different errors, some would clear the Problems tab but leave me with a red squiggle under views.html.index and auto-complete would not work with the scala.html templates.
What finally worked was to open the project's properties, go to Java Build Path > Source, and add both of the following directories:
target/scala-2.11/src_managed/main
target/scala-2.11/twirl/main
If you only do target/scala-2.11/twirl/main then you'll miss out on the class files generated from the conf directory.
In Scala IDE 4.0.0 thinks there's errors in an out-of-the-box Play Framework 2.3.7 program you can find the solution (in brief: adding target/scala-2.11/twirl/main folder to the compilation path), give it a try.
I had the same problem. I added target/scala-2.x/classes and target/scala-2.x/classes_managed to my Java build path and Eclipse stopped complaining.
Adding target/scala-2.11/twirl/main which is having views.html package to source fixed for me.
I had the same issue running Play 2.4.0-RC1 using default SBT layout (disablePlugins(PlayLayoutPlugin)) and solved it by adding to build.sbt:
sourceDirectories in (Compile, TwirlKeys.compileTemplates) :=
(unmanagedSourceDirectories in Compile).value
#brent-foust 's answer worked for me but only initially. Every time I rebuilt the project from within IDEA I would then get "not found: routes" errors from within target\scala-2.11\twirl\main\views\html\main.template.scala until I performed Brent's workaround again.
I eventually discovered the solution to that was changing a line in the .iml file from
<excludeFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" />
to
<sourceFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" isTestSource="false" />
I don't know what the long term implications of doing this are but it has fixed this problem. Some of the other similar problems mentioned might also be fixed by applying the same change to some of the other folders listed in the .iml.
I tried all solutions without any positiv result.
So I went to Preferences > Build, Execution, Deployment > Build Tools > sbt and checked Use sbt shell for imports and builds.
This let the compile button in intelliJ compile with the sbt shell.
I think this is better anyway, since a build server or something similar will compile the same way and not like intelliJ.
For me when importing the project into intellij making sure I "checked" the "auto import" checkbox did the trick.
1) Add the following line to your sbt.build file:
EclipseKeys.preTasks := Seq(compile in Compile)
2) Add the follwing line to your plugins.sbt file under the project folder:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")
3) Run the "eclipse" command from within sbt
as explained in the documentation of the play framework:
Setting up your preferred IDE
Basically we need a way to put the compiled classes on the path for this to work.
I did the following to fix it.
Since the projects compiles to the target directory.
I went to the Project Properties -> Java Build Path and added a few folders that look like this,
target/scala-2.12/routes/main
target/scala-2.12/twirl
target/scala-2.12/twirl/main
Now i dont want you to assume you will have these exact folders in your case too. That depends on your project setup.
But you should add the folders inside the target/scala-2.x folder.

sbteclipse not adding generated source folders to java build path?

I ran sbt eclipse on a Scala Project and when I imported it into Scala IDE(4.0.0 RC2), it gave me a type not found error as the types referred to were actually auto-generated code which were at target/scala-2_10/src_managed/main/compiled_avro/org/... I was able to do a sbt compile at the console though.
I got it to compile by adding the above folder to the Java Build Path.
My question is that since sbt eclipse can already detect Java Projects which the current project depends on and since sbt compile works at the console, should sbt eclipse be able to figure out dependencies to source folders of generated code as well? or maybe such a feature exists and I just don't know about it?
This may not be the correct way of doing things but to fix the issue i did the following.
sbt avro:compile
sbt compile
sbt eclipse
In eclipse i right clicked on target/scala-*/src_managed/main/compiled_avro > build path > use as source folder
The sbteclipse way:
Edit your project or global build.sbt file. My global ~/.sbt/0.13/build.sbt contains:
import com.typesafe.sbteclipse.plugin.EclipsePlugin._
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Managed
I'm using an older version of _sbteclipse, version 2.5.0 (various non-relevant reasons), which seems to require both the import and a single blank link between each line of real content (this drives me a bit crazy, yes). I don't believe the import is required for newer versions of sbteclipse.
sbt clean avro:compile compile
sbt eclipse

IntelliJ Scala configuration issue

So, I downloaded Scala and configured paths, I can run Scala console from terminal, Scala plugin is installed and "hello world" is running...
The problem is that, when I write a "hello world" program:
object First {
def main(args: Array[String]): Unit = {
println(12)
}
}
it says: Cannot resolve symbol println
As I said, I can run this program and it prints out 12... Also, if I create a List or an array it "cannot resolve symbol" but everything runs with no problem at all...
In most cases I've found, there was problem with Java set up, but that's not the case here...
Within File->Project Structure, make sure that there is a scala library in Project Settings\Libraries
Or, make sure that you have added scala-compiler.jar, scala-library.jar to your project.
If it is still acting strange, try File->Invalidate Cache/Restart
I had a similar issue with IntelliJ for an SBT project I started, with a correctly installed Scala 2.11 library, etc. Invalidate Cache fixed it so that IntelliJ could find the symbols.
Ensure, that you have scala-library.jar and scala-compiler in your project libraries.
Then try invalidating cache (File->Invalidate Caches/Restart->Invalidate and Restart).
If it's still not working, try reloading all your maven projects. You can either reimport them manually or go to Maven Projects->Reimport All Maven Projects (blue arrows).
I had similar issue and the last thing worked for me.
I hope it helps :)
I have the same issue when I use the idea 15, and fixed it in these 2 steps:
File -> Project Structure -> Libraries -> + -> Scala SDK -> your version -> OK
Maven Projects -> choose your scala project -> Lifecycle -> clean -> compile
Done
Don't know if this will help, but it worked with my environment. Navigate to:
File > Project Structure > Modules
Then, when I tried to apply a minor change, I got a message about how the Content Roots was being shared between two different Modules (a conflict). After removing the conflicting Content Root(s) from one of the Modules, IntelliJ started resolving symbols correctly.
You will see Content Roots on the right-hand side of the dialog box under module "Sources" tab.
I have no idea if the conflict in "Content Root" was what kept IntelliJ from resolving symbols, but fixing this error cleared everything up without having to change anything else.
if your version IntelliJ IDEA 2016.3.4
Worksheet configuration error:: Can't find Scala module to run
project structure > Modules > + > new modules > scala > scala
I had this problem - it turned out to be caused by me upgrading Java on my Mac, so that my JDK's path (/Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home) was no longer valid. I went into my Project Structure settings and updated the path to /Library/Java/Home, then the project could see the proper Java libraries.

Unable to make IntelliJ Idea 12 work with Scala compiler

I have installed Scala, sbt, eclipse and IntelliJ Idea 12. And also jdk, jre, etc. I'm able to run scala in Eclipse (Scala eclipse IDE) but I can't do it in Idea, even though I downloaded and installed scala plugin though Idea. Here is what I'm having at File -> Setting
and at a new project creation page
How do I solve these issues?
whereis scala
scala: /usr/bin/scala /usr/bin/X11/scala /usr/share/scala
which scala
/usr/bin/scala
I know I'm repeating this at any possible occasion—but your life will be much easier if you have sbt generate your IDEA project instead of trying to set it up manually. That will take care of configuring the modules correctly, so you are instantly ready to compile and run.
Here is a blog entry that might help. The section "How can I integrate libraries installed by SBT to IDEA?" tells you how to generate the project files.
Basically you need to create—starting from the root directory of your project—the file project/plugins.sbt with the following content:
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.4.0")
(you can also do that in the file ~/.sbt/plugins/build.sbt instead, that way you have the plugin available for any of your projects)
Then you run sbt gen-idea, and afterwards you can open the project directly from IDEA through File -> Open Project (and pointing to the project's root directory).
You could also generate your IDEA project with Gradle, which handles Scala+IDEA combination very well. Here's a minimal build.gradle script to do this:
apply plugin: 'scala'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.10.1'
}
Just create a directory for your project, put build.gradle inside it, create src/main/scala subdirectory, then install Gradle and run gradle idea inside your project's directory. That should generate nicely configured IDEA project. With this method you don't even need to install Scala.
What exactly your problem is? I don't see anything on your screens which prevents you from using Scala in IDEA. Just select "Set Scala Home" radiobutton in "New project" dialog and then select your Scala installation path (I guess it will be /usr/share/scala). IDEA then will automatically create library and compiler libraries and add Scala facet to your project.

Getting IntelliJ IDEA understand SBT dependencies

I have SBT and Scala installed and things work well.
Now I have an IntellIJ IDEA project, which works otherwise OK except that dependencies which are downloaded into /Users/MyName/.ivy2/cache/ are not understood.
I tried setting the cache path as a source folder:
but still things don't work out:
As you can see, org.eclipse is in red color meaning it can't find it. It must be inside that .jar file I showed on the left.
I just want to update/download dependencies using SBT, and then get the compilation to work in IntelliJ IDEA with those dependencies.
Use the sbt-idea plugin for SBT. Then you can run "gen-idea" from the sbt command line and it will generate the metadata files used by IDEA. These files contain the locations of jars as SBT uses them so that IDEA will know where the dependencies are, just like SBT.