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?
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!
I figured it out, right click on src > Mark directory as > Sources Root.
Now try again.
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.
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.
Now just click on the "Reload all sbt project" button.
This worked for me, I hope it will help someone.
Right Click on the SrcScala folder :)
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
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.
On IntelliJ IDE 2019.1 Ultimate, enable FRAMEWORK Scala like this screens:
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.
Go to Src folder and choose Mark as Directory then choose the option called source root, that would fix your problem.
add
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.50" in target -> built.sbt file
this works for IntelliJ IDEA 2017
Created new gradle project (java)
Add new module "scala" into /src/main/ and "Make Directory" as source ...
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'
}
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
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
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.
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.
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
You just need to select the 'Package' instead of 'Project' option from the drop-down menu just above your project name.
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
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.
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
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
Related
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.
In Eclipse I imported a maven-based project which uses maven jetty plugin. If I run mvn jetty:run from command line, everything works fine. If I add a run configuration in Eclipse and try to run it, I get the error message:
[ERROR] No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/home/eugene/.m2/repository), central (http://repo1.maven.org/maven2)] -> [Help 1]
In the Eclipe run configuration, I use:
Base directory: ${project_loc}
Goal: jetty:run
Maven Runtime: External
I read the [Help1] page. I don't have pluginGroup settings in maven configuration files, but I have the jetty plugin mentioned in pom.xml, so I guess everything should be fine (especially because everything works in command-line). I tried to "Run as > Maven clean" in Eclipse before executing the jetty run configuration, but it didn't help. Project compiles and passes all tests, only jetty:run does not work in Eclipse.
Please help, I'm an Eclipse & Maven newbie. Thanks in advance.
It does not work for me from command-line either.
Can you check if it works after adding the following in settings.xml as documented?
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
Also note that there are two different versions of the plugin - the older maven jetty plugin and the newer jetty maven plugin.
I met this problem too, an easier way to solve this is to edit your pom.xml, add following plugin:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.8.v20121106</version>
</plugin>
...
</plugins>
</build>
...
</project>
Note:
jetty-maven-plugin is used for jetty version 7 and above, if you want jetty version 6, you should use maven-jetty-plugin
for the version, you may want to have a look at here and here for your desired version's full name.
I apologize for wasting your time. Now I looked through maven warnings which appeared in Eclipse console after I ran the run configuration. I noticed
[WARNING] Failed to retrieve plugin descriptor for Plugin [org.mortbay.jetty:maven-jetty-plugin]: null
so it became obvious why it couldn't recognize jetty: prefix. Couple of lines above I saw a bunch of warnings about missing plugin versions. So I added a version specification for the jetty plugin (<version> entry in pom.xml) and it solved the problem. I forgot a common rule that if something breaks the first thing to check is warnings you get.
I've got this issue in eclipse after importing an appengine project.
The target:
mvn appengine:devserver
The error:
[ERROR] No plugin found for prefix 'appengine' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/averasko/.m2/repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]
The problem was in the incorrect base directory in the eclipse run configuration. When maven does not see a pom.xml file and is asked to run some non-standard target if fails like this as it don't know anything about the plugin that defines the target.
The solution is to correct the base directory to wherever your pom.xml file resides.
if you following this tutorial:
http://tapestry.apache.org/creating-the-skeleton-application.html
don't forget to follow this one crucial instruction:
Change into the newly created directory, and execute the command:
Since most IDEs are only able to import Maven projects, I'd like to generate a POM.xml from an SBT managed project, is there a better way to do it?
Did you try make-pom from sbt?
It generates basic POM for the current project at the ./target.
To customize generation, you can override pomExtra, pomIncludeRepository and pomPostProcess at the project definition.
There is a very direct way provided by SBT. You can use the below command where your SBT file exixts:
sbt makePom
This will generate the .pom file in the target folder you can search that and rename to pom.xml and keep that file in the location and run mvn clean compile install to get full out of it.
Vasil's answer is correct, but for Eclipse and IDEA you can generate IDE metadata more directly using plugins. For IDEA https://github.com/mpeltonen/sbt-idea, and for Eclipse https://github.com/musk/SbtEclipsify .
I am a beginner in Scala. I installed Scala IDE in eclipse and now I want to run my application programme. It never shows "run as Scala application", instead it shows "run as Java application" or "Java applet"
I opened "run configuration" and clicked on "Scala application" and my project name is "test" and second column is of "Class Main". What do I have to fill in? I filled in "Main.Scala", but it states "could not find mainmain class main.scala".
Can you help me with running this project?
If you want to run the whole project it should have a "main class", in any of your Scala objects you should be defining:
def main(args:Array[String]) { <some nice code here> }
From there it should be "calling" the rest of your objects to do whatever the whole project does and in the "Class Main" column you should specify the fully qualified name of your object. For instance, if you defined the main in a class called "Start" in the package "starter", in the "Class Main" field you should state "starter.Start".
But on the other hand if you only want to run a Scala object it should extend App, if it doesn't extend App, Scala IDE won't add the "Run as Scala Application...":
package greeter
object Hello extends App {
println("Hello, World!")
}
Right click your project and check the "Scala Compiler" settings. Check the "Project Specific" checkbox and try checking if you can run your Scala object (which should extend App).
make sure your declared package in your source code matches the directory structure under your source directory.
in this case, a sourcefile declaring package "greeter" will auto-run as scala if the source file is indeed under src/greeter/Hello.scala (and not just under src/Hello.scala)
Its a common mistake that doesn't get highlighted by the syntax checker.
If you installed Scala plugin for Eclipse, open the Scala perspective. Then right-click on your project and select "Add Scala Nature" in "Configure" menu.
You should now be able to run your Scala applications.
Download from this link Scala IDE
Restart Eclipse, create Scala Project, then create Scala Object and type this.
package greeter
object Hello {
def main(args:Array[String]) {
println("Hello, World")
}
}
Run as Scala Application
If it is the first time you run the Scala IDE for eclipse after setting it up and creating your project, all the thing you need is to just save your project and restart the IDE. At the next start, the "run as Scala Application" is appeared and can be used.
I had issues with the Scala IDE for Eclipse running Scala applications that extend Application, but running objects with a proper main method, i.e. def main(args:Array[String]) {/*...*/} always works fine for me.
Right click on the Project --> Click on Run Configurations --> In the Run Configurations window select the "Scala Application"
I was having similar issue. Make sure your java and scala files are not in the same package. I changed the package names and it worked for me
Unless you have a strong reason why you need Eclipse, could I recommend that you try IntelliJ?
Version 10 was just released earlier today, and the (free) community edition is perfectly happy working with the IntelliJ Scala plugin.
Just a pointer ..
I had faced same difficulty .
Being experienced from JAVA , instead of creating a Spark object I was creating spark class that why I was not getting this option .
Hope my experience helps .
Try to run the eclipse command of the sbt tool inside your project directory, this will build your scala project for the eclipse IDE. Then you will have no problem to configure your run configuration, It might even be done for you automatically.
$ sbt
> eclipse
[info] About to create Eclipse project files for your project(s).
[info] Updating {file:/...path-to-your-project}root...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Successfully created Eclipse project files for project(s):
[info] your-project-name
done! now import your project into Eclipse's workspace
I had this issue using an Eclipse Luna Scala IDE. No of the above solution made it possible to compile my Main.scala file.
package main.scala
object Main {
def main(args: Array[String]){
println("Hello, I am the main object")
}
}
The problem was the following: My project only referenced the JRE System library but no Scala library. I carried out the following steps:
Right click on project properties
Java Build Path
Check if the Scala library is missing
Click "Add Library..."
Add the desired Scala library that should have been shipped with the Scala IDE.
Then, go to the Main.scala file that is lying somewhere in your project folder and contains your main function. If you right-click file, "Scala Application" should appear under "Run as".
Following are the steps that I took to successfully run scala(Ubuntu) on eclipse:
1. Download Scala IDE
2. After installation, create a maven project.
3. right click on the project, go to configure and "Add Scala Nature"
4. In the .pom I provided the following dependencies:
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-graphx -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.12</artifactId>
<version>2.4.1</version>
</dependency>
Ensure that src/main/scala is on the build path.
Create a scala file to test your project.
Hope this helps!!!
I've created an IntelliJ Idea SBT Scala project like Heiko Seeberger's article describes.
I'd like to add a Jar library (joda-time) to use in this project. How to do this? Simply putting them into project's /lib does not help. If I right-click "External libraries" in Idea's project tree, "New >" is greyed.
In the IntelliJ Idea window of your project, got to File >> Project structure >> Libraries. After clicking that Libraries option, two panes will show up. At the top of the left-most pane, click the green "+" button.
The better way to do it is to add your unmanaged dependencies to your build.sbt and refrain from leaving part of your dependency management to your IDE.
Refer to http://www.scala-sbt.org/release/docs/Library-Management.html for details on how to define your unmanagedBase and unmanagedJars tasks.
Just Declare this in build.sbt
unmanagedJars in Compile += file(Path.userHome+"/Your-Jar-Path/Full-Jar-Name.jar")
and required jar will appear in External Library>unmanaged-jars>Full-Jar-Name.jar. This will also change if the jar file(in the provided path) is modified.
In Intellij Idea:
File > Project Structure > Libraries
In Netbeans:
File > Project Properties > Libraries
In Eclipse:
Right click the project > Properties > Java Build Path > Libraries
For a multi-module SBT project (Intellij 2017.3.4, Scala 12.2.4, sbt 1.1.1), the accepted solution only worked until restart or a project refresh. Indeed, "Project Settings-> Modules -> Dependencies", then "+" and "JARs or directories" gives a warning "Module X is imported from Sbt. Any changes made in its configuration may be lost after reimporting".
Possible workaround:
The suggestion by #zero worked for me as follows:
Put the JAR(s) into the project's
lib directory.
In build.sbt, inside lazy var baseSettings = Seq( ... ) add the line
unmanagedJars in Compile += file("YourPath/ProjectBla/lib/controlsfx-8.40.14.jar").
Still no luck? In the SBT tool window, in a module's sbt settings under unmanagedBase, unmanagedSourceDirectories (and the like) try calling the pop-up commands "Show value" and "Inspect" a few times. Somehow, it might work.
From Eugene Yokota's answer to How can I add unmanaged JARs in sbt-assembly to the final fat JAR? another option (which I didn't try) is to add an individual lib directory to each required module.
Hopefully, these steps will solve the problem or at least help debugging.
Create a lib directory in your project root path, paste your JARs in there, run sbt reload, close the project and open it again. Works for me in IntelliJ 2018.2.4 and SBT 1.0
Go to File -> Project Structure -> Modules
Select module in left pane and go to Dependencies tab
Click + to add JARs or directories
One way we can do is by adding jar explicitly in the Intellij but the problem with this approach is every time we build the project we need to add it again.
First way to do is :
In Intellij Idea:
File > Project Structure > Libraries
Another way is to publish the jar in local
Step 1 : Place the jar in any accessible folder.
Step 2 Create a build.sbt file which contains the detail of the jar.
Step 3 Open cmd to that path and execute command sbt publishLocal.
Step 4 Build your code and you would observe that you are able to access the classes belong to that jar.
build.sbt would look something like this:
organization := "com.abc.core"
name := "abc-test-logging"
version := "1.0-SNAPSHOT"
crossPaths := false
packageBin in Compile := file(s"${name.value}-${version.value}.jar")