150

I'm just starting to learn Scala. I have installed the Scala plugin for IntelliJ, and created a new Scala project. But when I right-click on the src folder to make a new Scala class, there is no option to do so. Am I missing something?

YakovL
  • 7,557
  • 12
  • 62
  • 102
Saurav Sircar
  • 1,743
  • 2
  • 12
  • 12
  • 1
    Have you specified the Scala SDK? It should work if you set up the project properly and specified the SDK. – evan.oman Jun 24 '16 at 06:38
  • 1
    I tried replicating the same issue but I get to make the scala file. I can suggest you a work around : Make a new Project and in scala select the sbt project and then inside the src.main make a new scala file. I hope that helps or you can try RESET the settings of intellij. – Shivansh Jun 24 '16 at 06:39
  • Like @evan058 said, it's the SDK. For some reason, IntelliJ does not pick scala sdk automatically sometimes. And if it's not configured, this happens. – Niks Jun 24 '16 at 06:55
  • Another possibility is that you're using the wrong path for Scala sources. They should go into `src\main\scala` or `src\test\scala`. If you have `sbt` installed, you can launch it in your project dir and invoke command `inspect scalaSource`, and it will show you where the Scala sources must be. – Haspemulator Jun 24 '16 at 12:47
  • Please see the comments inside other answers. A space in the project could be the reason of this error. – villancikos Jan 29 '17 at 14:15
  • Same issue - there is a blue folder under src>main that is called scala. That does bring up scala class when I right click but not the src folder. Has something changed in the IDE that is making the tutorials out of date or is this not right? – Reddspark Aug 31 '17 at 18:37
  • In my case (IntelliJ version 2018.3.2), if my project name contains uppercase letter, I encounter the same issue as OP. However, if I name the project all lowercase letters, the problem goes away. – Fanchen Bao Aug 19 '19 at 20:08

22 Answers22

307

Right click on your project, "Add Framework support" and choose Scala framework, then by right click on the packages you can create Scala Class.

After this, right click on src > Mark directory as > Sources Root.

Doing both of these should fix your problem!

Jsleshem
  • 715
  • 1
  • 10
  • 31
Majico
  • 3,810
  • 2
  • 24
  • 36
126

I figured it out, right click on src > Mark directory as > Sources Root.

Now try again.

Reddspark
  • 6,934
  • 9
  • 47
  • 64
25

I just had this issue, also. It turned out that IntelliJ hadn't marked my src/main/scala folder as a "source" folder.

To do this: Project Structure -> Modules -> right click folder and Mark as "Source" (blue)

Similarly the src/main/test folder wasn't marked as a test folder. I was able to add scala classes after those folders were appropriately marked.

Geoff James
  • 3,122
  • 1
  • 17
  • 36
Mike Pors
  • 318
  • 3
  • 5
11

I had the same problem when I created my first Scala project and I was able to solve it in a simpler way. Click on the "Search everywhere" button and type "sbt", or click directly on the "sbt" button located on the right near the edge of the screen.

sbt button

Now just click on the "Reload all sbt project" button.

reload all sbt project button

This worked for me, I hope it will help someone.

Rayees AC
  • 4,426
  • 3
  • 8
  • 31
Daniele F
  • 131
  • 1
  • 4
9

Right Click on the SrcScala folder :)

Animation of right-clicking "Scala"

Keith Pinson
  • 1,725
  • 1
  • 14
  • 17
  • 17
    The problem described is exactly what you're showing. No Scala Class in that menu. This animation is not helpful – Michael P Jul 26 '17 at 00:43
  • 3
    The animation demonstrates that the menus are context sensitive. Right clicking on the "scala" folder offers the option to select "Scala Class"; right clicking on the "src" folder does not. – Keith Pinson Jul 27 '17 at 00:31
  • So is this normal behavior and all the tutorials are out of date? – Reddspark Aug 31 '17 at 18:38
  • @user1761806 I have never reviewed any so I couldn't tell you if any tutorials are out of date. On that subject, I expect this answer about context-sensitive menus to become out of date, but as of InteliJ 2017.2 the animation is still illustrative even though the UI has changed. – Keith Pinson Sep 02 '17 at 13:43
  • 2
    Animation is not helpful. You can definitely have directories like /scala (and mark it has Sources Root) and still New Scala Class won't show up. Correct answer is by @Majico – Kumar Vaibhav Nov 06 '18 at 20:33
7

I had this problem everytime I created a new project with spaces in the name. eg "Hell Wev". Using "HellWev" or similar as a project name seems to work fine

cwadeevans
  • 139
  • 1
  • 4
  • 2
    I also had this problem starting a project name with a number. After renaming, everything worked fine. – user3846506 Jan 14 '17 at 23:11
  • Just to add my grain of salt. Yes, a space in the project folder creates this behavior. – villancikos Jan 29 '17 at 14:14
  • 1
    Had the same problem and @user3846506's comment was the answer for me, I recreated the project without a number in the name and the scala folder was blue instead of grey as soon as the project opened. – Shurikn Nov 10 '17 at 12:00
6

In IDEA 2016.2.5

From a new SBT or Scala project.

  • Open Project Structure
  • On the Project tab add your JDK (should also be visible in SDKs tab)
  • Under Global Libraries add Scala SDK.

Sadly just adding this to your build.SBT does not impact the IDE behavior.

Eric Aldinger
  • 125
  • 1
  • 6
6

On IntelliJ IDE 2019.1 Ultimate, enable FRAMEWORK Scala like this screens:

enter image description here

enter image description here

enter image description here

Claudio
  • 121
  • 2
  • 4
6

I had the same problem and what I did is Right Clicked on the Project ---> Then Choose Add Framework Support--> In the Left hand corner there was a list mentioned of Groovy, Kotlin, Maven and Scala.---> From the List I choose Scala but still had issue because the librabry was not specified--->Clicked on Create and it asked me to choose the version as I had Installed 2.11.12, i choose it and it started downloading it. Once it was downloaded I selected Scala and the issue was fixed.

4

Go to Src folder and choose Mark as Directory then choose the option called source root, that would fix your problem.

Sai Mammahi
  • 217
  • 2
  • 14
3

add libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.50" in target -> built.sbt file this works for IntelliJ IDEA 2017

techie95
  • 515
  • 3
  • 16
3
  1. Created new gradle project (java)
  2. Add new module "scala" into /src/main/ and "Make Directory" as source ...
  3. added dependencies into build.gradle:

    group 'example-scala'
    version '1.0-SNAPSHOT'
    
    apply plugin: 'java'
    apply plugin: 'scala'
    
    sourceCompatibility = 1.8
    
    repositories {
         mavenCentral()
    }
    
    dependencies {
       compile 'org.scala-lang:scala-library:2.12.6'
       testCompile 'org.scalatest:scalatest_2.11:3.0.5'
       testRuntime 'org.scala-lang.modules:scala-xml_2.11:1.0.6'
       testCompile group: 'junit', name: 'junit', version: '4.12'
    
    } 
    
27P
  • 1,183
  • 16
  • 22
3

In my case after selecting Sources Root it not shown me any Scala Class option then I choose Generated Sources Root, right click on src > Mark directory as > Generated Sources Root. This worked for me

GANESH CHOKHARE
  • 316
  • 3
  • 8
1

Intellij by default doesn't pick up the dependencies at times. You'll need to create a seperate sbt or maven project and then import it to Intellij.

If you want to create a Java Scala Mixin project you can follow this blog post. And then import it in Intellij.

Or if your just trying to learn Scala.

You can download this maven based Scala Starter Template and import it in intellij and then continuing working on it.

Note : You might have to specify the sources for the module in the

Project Structure -> Modules Tab

Vishnu667
  • 768
  • 1
  • 16
  • 39
1
    You can add the scala maven plugin reference in the pom.xml as below 

 <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaCompatVersion>2.11</scalaCompatVersion>
                    <scalaVersion>2.11.8</scalaVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>

    Once you do this and build you will be able to see the option of the scala class.
Nikunj Kakadiya
  • 2,689
  • 2
  • 20
  • 35
  • 1
    This one will work when you are creating project with mavan framework, and in case of SBT you have add build.sbt file – Chetan Hirapara Nov 15 '18 at 06:19
  • 1
    @ChicksPatel Yes it would work when you are creating project with maven framework only. As no specific framework was mentioned in the question I thought the answer might help someone. – Nikunj Kakadiya Nov 15 '18 at 07:17
1

I also faced same issue, the scala file options were not not visible in intellij Idea editor because Scala SDK was not installed. So you get rid of this issue after installing SDK for Scala.

1

Following the below steps resolved for me:

  • Add scala plugin to your Intellij IDE.(Plugins can be added by navigating to File -> settings -> plugins)
  • Enable scala plugin
  • Restart IDE
saikumar18
  • 15
  • 1
  • 5
0

You just need to select the 'Package' instead of 'Project' option from the drop-down menu just above your project name.

Nil
  • 1
0

I experienced this behavior when the folder was not correctly marked as a source folder (blue folder symbol). If that is the case, simply right-click on the whole project and choose Open Module Settings where you can then mark the respective folder as source: Module settings where you may select src as Source folder

Susie
  • 11
  • 3
0

I had a similar problem and what I did to resolve is just create a package by right clicking on src/main/scala. Once package is created, right click on package and you should be able to find scala class option.

0

In case someone else is having the same problem and the above didn't work, what worked for me was to:

  • Close IntelliJ
  • Delete the .idea folder
  • Open the project
ticot55
  • 47
  • 4
0

It's all about the jdk when creating a project. Click "download" in the corresponding Java selection column and it will select the version for you