0

There are a lot of posts about this error. Some of them worthy to mention are one two. But I think my case is different my main class is running before adding dependencies in XML and soon as I do that I get error Error: Could not find or load main class example.test123 I followed this video link to try basic example in Scala using Spark.I did following:

Step 1: Created the simple project testalpha

Step 2: a) Right click on the project name>> Go to configure>> Add scala nature

b) Copy the XML code from maven-repository paste this in pom.xml file. Not to forget to right click on Scala Library Container and change scala version to 2.10.6

Step 3: Add a folder scala such that we have src/main/scala

Step 4: Create a package example under src/main/scala. In this package Create a simple scala object and define main method to print hello. My question is:

If I don't add any dependencies in XML. It prints hello. But as soon as I add the dependencies I get this error Error: Could not find or load main class example.test123.

Does anyone have any idea about this? Screenshot to show project structure: enter image description here Screenshot showing printing of hello

enter image description here My pom.xml file:

<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>com.scalaproject</groupId>
    <artifactId>testalpha</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>testalpha</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>2.1.0</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Code Snippet:

package example

object test123 {
  def main(args: Array[String]): Unit = {
    print("Hello")
  }
}
Community
  • 1
  • 1
Paul Schimmer
  • 161
  • 3
  • 22
  • Please provide also your code-snippet – FaigB Mar 16 '17 at 14:28
  • This error generally come when you use class instead of object, if you want to execute your main method, you must use Scala object. – Kaushal Mar 16 '17 at 15:03
  • Kaushal I am using Scala Object – Paul Schimmer Mar 16 '17 at 15:27
  • I have this same issue now and its bothering me. I was actually getting a good uber jar with main scala class/object. But after I did a Maven->Update Project, the package disappeared and build no longer included main class. An other ideas are welcome. Glad your fixed. – Mike May 02 '17 at 01:52

1 Answers1

-1

I figured out the solution.Instead of directly downloading the scala-ide for eclipse from here make sure you download eclipse separately first from here.I guess there is still some version issue or some other bug which is why this scala-ide was not generating any class files. May that was the reason why it was unable to figure out the main class(quite surprising to see the target folder empty even after build).Anyways moving on, next you need need to install scala-plugin for newly downloaded eclipse-ide. You can do this as Window>>Eclipse-Marketplace>>Type scala>> Install.

Once it is installed we can follow the standard procedures to create the projects and then it works fine. I have posted relevant issue in the mailing-list of eclipse as well.

Paul Schimmer
  • 161
  • 3
  • 22