IntelliJ Scala configuration issue - scala

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.

Related

Problems compiling and running Scala projects in IntelliJ Idea

I have installed the latest IntelliJ Idea vertion with the Scala plugin. I have created a SBT Scala project with the IDE.
Unfortunately, I am not able to compile or run even the simplest "Hello World" example.
object Main {
def main(args: Array[String]): Unit ={
val x: Int = 5
println("Hello Scala!")
println(x)
}
}
If I try to run it, it says that "Error: could not find main class Main".
Rebuilding the project does not help.
BUT, if I run the "sbt" terminal program and execute "run", everything runs fine. Even more, it compiles the necessary class file so that IntelliJ Idea is able to run it after this step.
But, whenever I change something in the code and try to rebuild it from Idea, it will fail as before.
Edit: in Eclipse everything runs ok.
UPDATE
If I try executing "compile" from the sbt shell and then "Run" with the IDE, it will work. But, it will not do it while executing "Build" or "Rebuild Project". Sometimes, but unfortunately not always sp as to be able to reproduce it, it will throw me an exception with "Could not initialize class sbt.internal.io.Milli$"
UPDATE 2nd:
I have simplified even more the task. Now I have something like the following screenshot:
Screenshot IntelliJ IDEA
UPDATE 3rd:
There were two errors in my deployment. One of them was that, as it was pointed before, there was some package definition problems. All the source code should depend on src/main/scala, but that is not enough. So as to use the SBT structure for construction, I had to go to
File -> Settings -> Build, Execution, Deployment -> Build Tools -> sbt
and check "Use auto-import" and "use sbt shell for build and import".
After that, everything runs ok. Finall!
Thanks a lot tro everyone for the useful input!
On your screenshot Runner is in wrong package. It's written package main.scala.
If Runner.scala is in src/main/scala/way/to/my/package package declaration should be package way.to.my.package (if it's just in src/main/scala there should not be line package ...).
Also if still necessary you can try (from what should be tried first to what should be tried last, if things tried before didn't help)
sbt clean
reimport the project to IntelliJ IDEA
File -> Invalidate Caches / Restart ...
delete .idea subfolder of project folder
Did you try the green arrow near "object Main"?
This should start the program.

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

`Cannot find symbol` in IntelliJ 14

I have use the IntelliJ Scala plugin before, in 13 and 13.1. I upgraded to 14, and it doesn't work anymore for my SBT project.
For all Scala standard lib stuff, I see errors like "Cannot find symbol scala.Option".
at scala project, compiler error - Cannot resolve symbol List? says I need to have the Scala facet for my module. I looked in facets, and Scala wasn't an option.
I've uninstalled IntelliJ and the Scala plugin and my settings and the projects files multiple times, but still happens.
How do I fix this?
the new scala plugin for intellij 14 removed the facet and replaced it with Scala SDK library, see blog
for sbt project, I guess the best bet is to re-create your project by:
File -> open -> select the build.sbt of your project in popup -> delete existing project and import
I had a similar issue when a Java module calls an Scala object. The issue was from the wrong setup in Source Folders; the Scala source was in src/main/scala/..., but in the Project Structure, the Source Folders were setup as src by default. When I changed it as src/main/scala, the Java module can find the Scala object correctly.
I fixed this by using the nightly builds of the Scala plugin.
The fixes have now been incorporated into the stable versions.
In my case i just had to reload the IDE...

Error: scala: No 'scala-library*.jar' in Scala compiler library

Environment: Play 2.3.0/Scala 2.11.1/IntelliJ 13.1
I used Typesafe Activator 1.2.1 to create a new project with Scala 2.11.1. After the project was created, I ran gen-idea. The generated IDEA project fails to compile with the error:
Error: scala: No 'scala-library*.jar' in Scala compiler library in test
Am I doing something wrong? Workaround?
Open File -> Project Structures -> Libraries, remove any scala sdk in it, e.g. scala-sdk-2.11.8 in the following image.
Click on +, then Scala SDK.
Select the right Scala SDK from the list, for me, it's Ivy-Scala-2.11.8 shown in the picture.
Select the current project(mine is spark-test), click OK.
Then click OK to close it.
Run again, it should work now.
Since IDEA 13 you should use SBT support which is bundled with Scala plugin.
With it there is no need to add third-party SBT plugins and run special commands; just import the project using "File -> Import project..." menu item, and it will automatically load SBT project structure and its dependencies.
I had the same issue with the .idea files generated by ./activator idea with play 2.3. A quick fix is to look in "Project Structure->modules" and note the name of the compiler library. For me it was "SBT: scala2.11.1". The look in "Project Structure->Libraries" and check the contents of this library. It should contain scala-compiler.jar, scala-library.jar, scala-reflect.jar. If this libraries are not present, add them (in my case they are located in ~/.sbt/boot/scala-2.11.1/lib).
For IDEA 15 and project exported from Activator 1.3.5 only Russell's solution helped at the moment.
I have fixed scala compiler library with adding 3 libraries located in ~/.sbt/boot/scala-2.11.1/lib to library's compiler classpath and classes. After indexing project I moved this library to global libraries of IDEA and it was saved there.
I had the same issue.
If you are building your project using external sbt i.e sbt compile then it will create or add dependencies in libraries(all external dependency also) and scala sdk will be added as SBT:scala* based on version of your scala.
So you can remove SDK with SBT as prefix and add proper SDK through Intellij.
File->Project Structure->Libraries
Remove SBT SDK and add it manually.
For me with IDEA 12, I fixed this issue when I unchecked the box for "Use external build" in Settings.Compiler.
now is 2021 year, in Mac, use IntelliJ to run scala:
core logic
Mac: brew install scala
IntelliJ: open *.scala file, then according notice to auto config java JDK and Scala SDK
detailed steps: pls refer another post's answer
I'm an absolute Scala beginner. I was wanting to get up and running. I needed to create a Scala project, not a Java project with Scala library:

noClassDefFoundError using Scala Plugin for Eclipse

I successfully implemented and ran several Scala tutorials in Eclipse using the Scala plugin. Then suddenly I tried to compile and run an example, and this error came up:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/HelloWorld
Caused by: java.lang.ClassNotFoundException: hello.HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
After this point I could no longer run any Scala programs in Eclipse. I tried cleaning and rebuilding my project, closing and reopening my project, and closing and reopening Eclipse.
Eclipse version number 3.5.2 and Scala plugin 2.8.0
Here is the original code:
package hello
object HelloWorld {
def main(args: Array[String]){
println("hello world")
}
}
If you see this when you attempt to run as a Scala application then the most likely explanation is that your project didn't compile and no class files were generated. Please check whether or not that's the case: look in your project's output folder for hello/HelloWorld.class.
If your project didn't compile that could either be because there's an error which you've missed (and if this error isn't being reported in the Problems view that could be a bug, in which case please open a ticket on Trac) or because you've turned off automatic builds and not done a manual build of your project.
I had the same problem. Project doesn't compile but there are no errors highlighted and AFAIK the code is OK. It seems to be a problem with the Run Configurations.
Solution 1: Delete the existing Run Configuration for your object and create a new one
Solution 2: Create a new object and cut / paste all your code into that file
When running "clean" does not un-hose Eclipse, I next try saving my work, exiting Eclipse, and re-starting. That usually gets things going again, but not always. A few times I've had to update the Scala plugin with a more recent version (I'm using the latest nightly), to get things working again. I doubt that this worked because the new plugin happened to fix the bug, but rather expect that loading the new plugin gives the whole Eclipse-Scala
system a "total reset" that gets it unhosed.
I was getting this problem in a project that combined .java & .scala files.
The solution for me was:
Remove all .java files
Edit the scala code as needed so it compiles without them.
Add the .java files back in.
Edit the scala code back.
The other solutions given here didn't work for me. I tried: clean project, restarting Eclipse, closing-&-opening the project, creating a new .scala file. No joy.
I'm using Eclipse 3.7 (latest stable), Scala IDE 2.0.0 and Scala 2.9 on Ubuntu Linux 11.10.
The symptoms in my case were:
My project was working, but then it stopped compiling for no apparent reason. The IDE didn't show any compilation errors for .scala files, but there were no .class files in the output directory & I got a NoClassDefError if I tried to run anything.
If I created a deliberate error in a .scala file, that did get picked up as a compilation error.
The .java files were registering errors due to the missing scala classes.
I suppose there's probably a boot-strapping bug somewhere in the IDE plugin for .java/.scala mixes. I've done hybrid projects with this setup without problems, so it's only triggered in some situations. I don't know what the trigger is, but once triggered, there's no nice solution.
I had moved my one and only class/object/application to a package, but had not added the package declaration.
sbt compiled and ran fine; eclipse would not
Adding the package declaration at the top of the file fixed it.
Scala 2.8.3 plugin; no compile error
I encountered this error too but after doing the suggestions here (cleaning, deleting Run Configuration etc), I realized that I set the workspace wrongly that is why the class is not being found.
An indication that this is a problem is when the same error occurs when you try to compile a java project.
I encountered this error (compilation worked in sbt but failed in eclipse) when I created a new package object called "common". Deleting the package object in eclipse caused the compile error to go away. There was nothing in it.
I was using sbt-eclipse to build the eclipse project. I'm using scala eclipse 3.0.0-vfinal-20130326-1146-Typesafe.