0

If I create a Scala project in Spring Tool Suit with a scala object called HelloWorld.scala with just the following lines:

package ScalaMavenTest

object HelloWorld {
 def main(args: Array[String]) {
    println("Hello world !!!")
  } 
}

And then I try to run it, everything goes fine and print out "Hello World !!!"

However, if I convert it to maven STS can't find the main class anymore. Here are the steps I followed:

1-I Converted the Scala Project into Maven:

Right click on the project -> Configure -> Convert to Maven Project -> Finish

2- I changed the standard pom.xml by:

<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>ScalaMavenTest</groupId>
    <artifactId>ScalaMavenTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-tools Maven2 Repository</name>
            <url>https://oss.sonatype.org/content/groups/scala-tools/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala-tools Maven2 Repository</name>
            <url>https://oss.sonatype.org/content/groups/scala-tools/</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>maven-scala-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                </plugin>
                <plugin>
                    <groupId>org.scala-tools</groupId>
                    <artifactId>maven-scala-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>add-source</goal>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

3 - Right Click on Project -> Run as Maven Build... -> goals: clean install

4 - I try to run the Scala Object as Scala Application:

4.1 Right Click on Project -> Properties -> Scala Compiler -> select Use Project Settings -> Scala Installation -> Fixed Scala Installation 2.11.8 (built-in)

4.2 Right click on Scala Object -> Run As -> Run Configurations -> Double Click on Scala Application:

     Project: ScalaMavenTest

     Main Class: ScalaMavenTest.HelloWorld

However the output is the following error:

Error: Could not find or load main class ScalaMavenTest.HelloWorld

This questions was already well asked two years ago here:

Running a scala application in maven project

But I tried all the given answers and I still have the same error. I guess the possible solutions need an update or I am missing something. Any extra recommendation?

Extra Details:

Spring Tool Suit: sts-3.8.2.RELEASE

Eclipse Marketplace -> Installed: -Maven (Java EE) Integration for Eclipse WTP (Luna/Mars) 1.2.3 -Scala IDE 4.2.x

1 Answer 1

0

Well I found a solution that apparently works. (But can't explain 100% why)

When you create a maven project the structure of the project is something like:

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

However that structure is not generated automatically when you convert a Scala Project to Maven (By doing Configure -> Convert to Maven Project)

So what I did was to create the folder structure on my own by adding two folders inside src -> main -> java (nested)

Then I moved the HelloWorld.scala into that java folder.

Finally:

Right click on Scala Object -> Run As -> Run Configurations -> Double Click on Scala Application:

 Project: ScalaMavenTest

 Main Class: ScalaMavenTest.HelloWorld
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.