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

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.

Related

Multiple scala libraies causing error in intellij?

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

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.

Intellij unable to resolve references to a specific jar file

In a given project that is driven by sbt there is some kind of corruption in the project libraries specifically for a MavenLocal repository used for kafka-spark-9.7.2.jar in which:
references to the classes provided by that jar are marked as "symbol not found" by the editor parser
however the editor (strangely) does offer to import the classes
but after accepting the import, the symbols are still marked in red as unresolved.
The following attempts to "clean things up" have already been performed:
Build | Make Project
Build | Rebuild Project
In addition I have verified that the project does build from
sbt package
on the command line
UPDATE After re-running sbt gen-idea the librraries are still not found by the Parser. yet the libraries exist -even IJ knows about them as shown in the following screenshot. Why is it that IJ can find the library
C:\Users\S80035683\.ivy2\cache\org.apache.kafka\kafka\jars\kafka-0.7.2-spark.jar!\kafka\api\FetchRequest.class
However IJ is unable to resolve any classes from that library in the Parser?
You have to build the project for Intellij, try to do this in your project root:
sbt idea with-sources=yes
This should build the project structure from scratch and add the right dependencies, usually I refresh the project after adding a dependency or a jar.
Edit:
To use the command you need this plugin, otherwise you can use gen-idea but I used it only a few times and I'm not sure how it will work out.
Edit2:
There was some confusion, first, for the IDEA SBT console you don't need to prepend the sbt command since you already are inside sbt:
If you have the sbt plugin for idea you can use gen-idea with-source=yes (without prepending sbt)
From the terminal, either you go to your project root and type sbt to enter the sbt console and use gen-idea or idea with-sources=yes (without prepending sbt)
or directly sbt gen-idea or if you have the plugin sbt idea with-sources=yes (prepending sbt)
To reach the sbt console inside idea you need to install the sbt plugin on preferences -> plugin and search for sbt and then View -> Tool Windows -> SBT Console:
To start the console click on the play button, to kill the console on the skull.
I had the same problem. I fixed it by directly writing the CLASSES and SOURCES of the problematic library. This can be found in .idea/libraries/SBT__<problematic library>_jar.xml

Eclipse unresolved symbol with Play Framework

I'm a beginner user of Play Framework 2.1.3 and I have just created a new Java application and I have run play eclipse to generate the eclipse project for it. I have also tested so that it works by doing a play run.
So I'm following this tutorial and there is a step where you should add this piece of code:
public static Result index() {
return redirect(routes.Application.tasks());
}
But I'm getting the message "routes cannot be resolved". I have also tried play compile and in Eclipse doing a clean to no avail.
I was having the same trouble after the recent 2.4.X release of Play and the solution of cleaning/compiling/reimporting wasn't working. The solution for me was to:
Add the below keys to build.sbt
Kill eclipse
./activator clean
./activator compile
./activator eclipse
Re-import into eclipse
The problem is basically that the managed source directory wasn't being created, these lines fix the problem.
EclipseKeys.projectFlavor := EclipseProjectFlavor.Java // Java project. Don't expect Scala IDE
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources) // Use .class files instead of generated .scala files for views and routes
EclipseKeys.preTasks := Seq(compile in Compile) // Compile the project before generating Eclipse files, so that .class files for views and routes are present
run play clean-all from your project directory
run play eclipse from your project directory
refresh your eclipse project
Upgrade sbteclipse to version 5.1.0 (which was released on January 12th 2017) - it fixes this bug.
Probably some kind of classpath issue in your Eclipse setup. Anyway, you should not rely on Eclipse to compile your Play application, simply compile it from the console and use Eclipse for source code editing only. (BTW : there are way lighter IDEs then Eclipse if you don't use it for compilation)