raghav
raghav

Reputation: 629

configure run in eclipse for Scala

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?

Upvotes: 60

Views: 69314

Answers (14)

prashant.kr.mod
prashant.kr.mod

Reputation: 1682

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>
  1. Ensure that src/main/scala is on the build path.
  2. Create a scala file to test your project.

Hope this helps!!!

Upvotes: 0

MichaelHuelsen
MichaelHuelsen

Reputation: 406

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:

  1. Right click on project properties
  2. Java Build Path
  3. Check if the Scala library is missing
  4. Click "Add Library..."
  5. 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".

Upvotes: 0

Jules0707
Jules0707

Reputation: 605

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

Upvotes: 0

sparkDabbler
sparkDabbler

Reputation: 525

Right click on the Project --> Click on Run Configurations --> In the Run Configurations window select the "Scala Application"

Run Configurations Window

Upvotes: 2

Sandeep Das
Sandeep Das

Reputation: 1050

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 .

Upvotes: 0

nir
nir

Reputation: 3868

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

Upvotes: 1

jimakos17
jimakos17

Reputation: 935

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

Upvotes: 5

Ram Janovski
Ram Janovski

Reputation: 181

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.

Upvotes: 15

Serendipity
Serendipity

Reputation: 65

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.

Upvotes: 4

Rajgopal C
Rajgopal C

Reputation: 812

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

Upvotes: 24

LMDDGTFY
LMDDGTFY

Reputation: 557

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!")
}

Upvotes: 50

Fabian Steeg
Fabian Steeg

Reputation: 45714

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.

Upvotes: 2

Kevin Wright
Kevin Wright

Reputation: 49705

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.

Upvotes: 0

jopasserat
jopasserat

Reputation: 5920

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.

Upvotes: 9

Related Questions