简体   繁体   中英

Error: Failed to Load Class main using Spark-submit

My Code is below

import org.apache.spark.SparkContext;
import org.apache.spark.SparkConf;
object WordCounter {
   def main(args: Array[String]) {
     val conf = new SparkConf().setAppName("Word Counter").setMaster("local")
     val sc = new SparkContext(conf)
     val textFile = sc.textFile("C:/spark/README.md")
     val tokenizedFileData = textFile.flatMap(line=>line.split(" "))
     val countPrep = tokenizedFileData.map(word=>(word,1))
     val counts = countPrep.reduceByKey((accumValue,newValue)=> accumValue + newValue)
     val sortedCounts = counts.sortBy(kvPair=>kvPair._2,false)
     sortedCounts.saveAsTextFile("C:/Data/WordCountViaApp")
   }
}

Can someone please help?

C:\Users\workspace\SparkInScala>spark-submit --class "WordCounter" "C:\Users\workspace\SparkInScala\target\scala-2.12\sparkinscala_2.12-0.1.0-SNAPSHOT.jar" Error: Failed to load class WordCounter.

I meet the same problem before.

I'm using Maven plug-in of idea to develop Spark Program.Here is my pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.2</version>
            <executions>
                <execution>
                    <id>compile-scala</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile-scala</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <scalaVersion>2.12.11</scalaVersion>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>testConf</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <includeProjectDependencies>true</includeProjectDependencies>
                <includePluginDependencies>false</includePluginDependencies>
                <classpathScope>compile</classpathScope>
                <mainClass>testConf</mainClass>
            </configuration>
        </plugin>
    </plugins>

</build>

you can try it by replace the mainClass label "testConf" to your class name,and scala version etc.

If you want to use SBT then you have to specify which main method to start the execution.

Main-Class: com.WordCounter.main

That above tells SBT to add the line to the META-INF/MANIFEST.MF file in your JAR when you run sbt package.

When running your application with SBT,

sbt "run-main com.WordCounter.main"

Also, as pointed out by Wu, Maven will do the job as well.

You should include the full path of the.scala class/file that you want to execute. For example if your class resides in a folder structure like com/example/xyz.scala then your command should be: "--class com.example.xyz"

This is working fine after making changes in build.sbt file with appropriate versions. I no more see the error Failed to Load class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM