Maven SPARK-java.lang.ClassNotFoundException

Maven SPARK-java.lang.ClassNotFoundException,maven,apache-spark,Maven,Apache Spark,我正在尝试使用spark submit运行jar文件 han-ui-MacBook-Air:spark-2.2.0-bin-hadoop2.7 Alphahacker$ ./bin/spark-submit --class SimpleApp --master local[*] /Users/Alphahacker/IdeaProjects/sparksimpletest/target/spark-simple-test-1.0.jar 但我得到的错误信息如下: java.lang.Class

我正在尝试使用spark submit运行jar文件

han-ui-MacBook-Air:spark-2.2.0-bin-hadoop2.7 Alphahacker$ ./bin/spark-submit --class SimpleApp --master local[*] /Users/Alphahacker/IdeaProjects/sparksimpletest/target/spark-simple-test-1.0.jar
但我得到的错误信息如下:

java.lang.ClassNotFoundException: SimpleApp
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.spark.util.Utils$.classForName(Utils.scala:230)
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:712)
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:180)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:205)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:119)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
我的IntelliJ屏幕如下所示: ,以及本项目的pom.xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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>alpha.spark.test</groupId>
    <artifactId>spark-simple-test</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>spark-streaming-twitter_2.10:spark-streaming-twitter_2.10</artifact>
                                    <includes>
                                        <include>**</include>
                                    </includes>
                                </filter>
                                <filter>
                                    <artifact>twitter4j-stream:twitter4j-stream</artifact>
                                    <includes>
                                        <include>**</include>
                                    </includes>
                                </filter>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
我不知道怎么了。 正如您在上面的图片中所看到的,我编写了SimpleApp对象而没有包,并且使用Maven构建了项目。我认为我的命令是正确的


请帮我解决这个问题。

您必须使用此文件夹结构将java文件保存在目录中
src/main/java/example.java
并使用maven构建。此方法解决了我的错误,这与您的错误非常相似。

您必须使用此文件夹结构
src/main/java/example.java
将java文件保存在目录中,并使用maven进行构建。这个方法解决了我的错误,这个错误与你的非常相似。

你能显示你的程序代码吗?也许您的
SimpleApp
类在一个包中?然后您必须在
--class
选项中指定完整的限定名。是否
simpleap
没有包?如果没有在命令行中提供完全限定的类名,比如
spark submit--class com.spark.example.SimpleApp
@hage,我只是在帖子中添加了程序代码。正如您所见,SimpleApp类不在任何包中。@diginoise SimpleApp不在任何包中。我刚刚在帖子中添加了SimpleApp代码。你的程序在我的机器上运行良好。但是我没有maven就做了。你能展示你的程序代码吗?也许您的
SimpleApp
类在一个包中?然后您必须在
--class
选项中指定完整的限定名。是否
simpleap
没有包?如果没有在命令行中提供完全限定的类名,比如
spark submit--class com.spark.example.SimpleApp
@hage,我只是在帖子中添加了程序代码。正如您所见,SimpleApp类不在任何包中。@diginoise SimpleApp不在任何包中。我刚刚在帖子中添加了SimpleApp代码。你的程序在我的机器上运行良好。不过,我在没有maven的情况下建造了它。
import org.apache.spark.{SparkContext, SparkConf}

object SimpleApp {
  def main(args: Array[String]) : Unit = {
    val conf = new SparkConf().setAppName("SimpleApp").setMaster(args(0))
    val sc = new SparkContext(conf)
    val filePath = "/Users/Alphahacker/Desktop/README.md"
    val inputRDD = sc.textFile(filePath)
    val matchTerm = "spark"
    val numMatches = inputRDD.filter(_.contains(matchTerm)).count()
    println("%s lines in %s contains %s".format(numMatches, filePath, matchTerm))
    System.exit(0)
  }
}