0

I am trying to run some trivial Scala code in Eclipse but I am not able to get it working when Maven is enabled.

I start by creating a Scala project in Eclipse and then I add an object with the following code:

object HelloSpark extends App 
{
    println("Hello, world!")
}

I then use "Run As>>Scala Application" to successfully execute the above code:

Hello, world!

I then enable Maven by clicking on "Configure>>Convert to Maven Project". While the ensuing build is successful, the "Run As>>Scala Application" option no longer comes up. Using the previously created launch configuration results in the following error:

Error: Could not find or load main class HelloSpark

Trying the answer for Eclipse, Scala & Maven - Class files are not generating did not help in my case.

Could someone please tell me what I am doing wrong? I don't understand why enabling Maven changes the way Scala code is run.

Thanks.

Edit: Here is the pom.xml that was automatically generated when I converted the above to a Maven project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>HelloSpark</groupId>
  <artifactId>HelloSpark</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

Community
  • 1
  • 1
Vishakh
  • 1,168
  • 1
  • 11
  • 20

2 Answers2

0

Usually when you have a maven project, the project is build with maven. Therefore you need a pom.xml file where you define how to build the project and so on. Are you aware of this, did you configure a pom.xml? Without the correct pom file you cannot build and run the app.

So without a correct maven build, you will not have a class file. Therefore you are getting this error: Error: Could not find or load main class HelloSpark

AlexL
  • 761
  • 1
  • 6
  • 20
  • Converting the project to a Mven project automatically added a pom.xml file. I have added it to my post as an edit. – Vishakh Nov 18 '15 at 19:09
0

So it turns out simply converting the Eclipse project to a Maven project is not enough. Maven has to know it's a Scala project and build it accordingly.

I followed the instructions at http://docs.scala-lang.org/tutorials/scala-with-maven.html and now I have a working project with running code. It's important to generate a Maven archetype first and then convert it to an Eclipse project before importing it into Eclipse.

Vishakh
  • 1,168
  • 1
  • 11
  • 20