configure run in eclipse for Scala - eclipse

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!!!

Related

Import Twitter/chill to Scala code using Intellij IDE

I have to do some assignments with Scala and I'm a newbie for this language.
In the assignment, the prof requests me to implement serialization and deserialization, using twitter/chill.
https://github.com/twitter/chill/
However, I don't know how to import the libary into my IDE Intellij.
Each time I use val instantiator = new ScalaKryoInstantiator.
IDE notify me that: Cannot resolve symbol ScalaKryoInstantiator.
Could anyone can help me to resolve this issue?
Thanks and Best Regards,
Long.
One way to add a library in a Scala project, if you use sbt, is to add an sbt dependency. Most libraries and jars can be found and downloaded from the maven repositories: https://mvnrepository.com/artifact/com.twitter/chill_2.9.2/0.2.3
If you click the sbt tab on the page above, maven gives you a code snippet which can be directly pasted into your build.sbt. In order to compile and run your project using sbt, open the terminal in the directory where your build.sbt is located and use the "sbt run" command.
If you don't use sbt, download the jar from maven and follow the instructions in this answer: How to add external library in IntelliJ IDEA?

Unable to create Scala class on IntelliJ

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

How do I find the correct Maven archetype project for developing with Scala in Eclipse?

I'm having problems trying to just create a Maven project using Scala (v2.11.6) within Eclipse (Luna). The instructions here says to install both plugins below.
Maven Integration in Eclipse
m2eclipse-scala connector
Both plugins can be installed by using the m2eclipse-scala update site. So here's what I did in my first attempt.
Download Eclipse Luna (for JavaEE developers)
Install "both" plugins above by using the m2eclipse-scala update site.
When Eclipse loads up, I attempt to create a Maven project, and the archetype I want to use is Group Id=net.alchim31.maven, Artifact Id=scala-archetype-simple, Version=1.5. However, this archetype never shows up when I enter in "scala-arch" in the filter text field.
The only project I see is the one with Group Id=org.scala-tools.archetypes, Artifact Id=scala-archetype-simple, Version=1.2. When I select this archetype to use, I get a bunch of error messages in Eclipse.
error while loading ConsoleRunner, Scala signature ConsoleRunner has wrong version expected: 5.0 found: 4.1 in ConsoleRunner.class
error while loading JUnit4, Scala signature JUnit4 has wrong version expected: 5.0 found: 4.1 in JUnit4.class
error while loading Specification, Scala signature Specification has wrong version expected: 5.0 found: 4.1 in Specification.class
In my second attempt, I try to download the pre-packaged bundle (Scala IDE for Eclipse). Again, when I attempt to create a Scala Maven project, I don't see the Maven archetype for Group Id=net.alchim31.maven (only for Group Id=org.scala-tools.archetypes).
Any ideas on what I'm doing wrong here? Or where I can find a vanilla Scala Maven project to import/modify and use for my own purpose?
Here's how I got it to work. It seems all the archetypes aren't available, so based on this answer, I created a remote catalog
Before
So do this
Go to [Windows] → [Preferences] → [Maven] → [Archetypes] → Add Remote Catalog and create the catalog, then Apply
For copy-pasting:
Catalog File: http://repo1.maven.org/maven2/archetype-catalog.xml
Description: Remote Archetypes
Go back to creating a new Maven project. Select the Remote Catalog from the dropdown. It will take a few moments to gather all the archetypes. You can see the progress at the very bottom right of the IDE. When it's done, you should be able to see the archetypes
After
Create your project with groupId and artifactId and such.
Right click the project, then from the context menu [Run As] &rarr [Maven Build]. In the dialog, type clean package into the goals. The run. The project should build and run the tests.
For me at first I got an error on the build. It was because my default environment is using Java 8. I'm pretty new to Scala, so I'm not sure is there is a problem with Scala and Java 8 or not (I think it's the Scala version in the pom (2.10.0)). But what I did to get it to work was just change the Java version used in the IDE to Java 7.
Basically just go to [Windows] → [Preferenes] → [Java] → [Installed JREs] → Add → [System VM] → Next → Directory → Find the directory of the Java home (version 7) → Finish → Then in the list tick Java 7.
Then build again. It should work. Good Luck!
One point I want to add, If you are getting following error while installing the archetype showed in image 1
Can't resolve Archetype
org.glassfish.jersey.archetypes:jersey-quickstart-webapp:2.21
org.eclipse.core.runtime.CoreException: Could not resolve artifact
org.glassfish.jersey.archetypes:jersey-quickstart-webapp:pom:2.21
Image 1:
Then before procedding with the solution specified above, try to complete these steps in eclipse.
I faced the same problem and chanced upon this SO question, as well as the answer, given by #peeskillet (many thanks). I am using:
Maven 3.3.3
Eclipse Luna Scala IDE 4.0.0.-vfinal-20150305-1644-Typesafe
Scala 2.11.6
JDK 1.8
After following the steps exactly as given here, I hit a wall. When I chose the right archetype (net.alchim31.maven), I was greeted with this error:
Could not resolve artifact net.alchim31.maven:scala-archetype-simple:pom:1.5
Suspecting that the maven embedded in eclipse was acting funny, I visited [Windows] -> [Maven] -> [Installations] and added local installation of maven (whatever is set to M2_HOME in my shell). But, no luck!
Exasperated, I visited this site: Scala Doc and switched back to command-line as this page instructs. Surprisingly (to me, at least), it worked. A maven-Scala project was created in the present directory, which I could import into Eclipse.
Just so that I am not misunderstood, I am not suggesting that #peeskillet's answer is wrong. Just wanted to share another way of creating a maven-scala project on Eclipse, in case it helps someone. I have not yet understood, why Eclipse-Scala-IDE is yet to give that 'Aha' feeling.
After you have successfully created the scala-maven project as described above, you will probably see an error "Type not found: type JUnitRunner specs.scala". In that case, manually add the following XML snippet to your pom.
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
Moreover, to avoid errors when running mvn test,
[ERROR] scalac error: bad option: '-make:transitive'
You also need to delete the following line from the pom.xml file:
<arg>-make:transitive</arg>

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:

sbt eclipse command changed files and packages

I created a new Scala project in eclipse then added a package and Scala object ,
So far so good ...
i want to add external library so i added a project folder with build.properties plugins.sbt files,and another file build.sbt in the root project.
in the terminal i compiled successfully the project with the sbt compile task.
the problem is that after sbt eclipse command the eclipse project changed from Scala project to something else.... all the packages changed to simple folders and the Scala project is ruined
scala IDE :Build id: 3.0.3-20140327-1716-Typesafe
scala version :2.10.4
sbt version:0.13.0
you can see in the image
Where did you get the eclipse command from? I'm guessing you're using sbteclipse.
I created a new Scala project in eclipse then added a package and Scala object , So far so good ...
If I understand correctly, this is exactly the opposite of what the plugin is intended to do. I think you're suppose to create a plain sbt project, and then let the plugin generate the Eclipse project.