【问题标题】:How to spark-submit with main class in jar?如何在 jar 中使用主类进行火花提交?
【发布时间】:2018-05-19 08:10:10
【问题描述】:

有很多关于ClassNotFoundException 的问题,但我还没有看到任何(还)适合这种特定情况的问题。我正在尝试运行以下命令:

spark-submit --master local[*] --class com.stronghold.HelloWorld scala-ts.jar

它会抛出以下异常:

\u@\h:\w$ spark_submit --class com.stronghold.HelloWorld scala-ts.jar                                                                                                                                                                                                                                                                               ⬡ 9.8.0 [±master ●●●] 
2018-05-06 19:52:33 WARN  Utils:66 - Your hostname, asusTax resolves to a loopback address: 127.0.1.1; using 192.168.1.184 instead (on interface p1p1)                               
2018-05-06 19:52:33 WARN  Utils:66 - Set SPARK_LOCAL_IP if you need to bind to another address                                                                                       
2018-05-06 19:52:33 WARN  NativeCodeLoader:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable                                
java.lang.ClassNotFoundException: com.stronghold.HelloWorld                               
        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:235)                     
        at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:836)                                                                  
        at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:197)        
        at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:227)             
        at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:136)               
        at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)                    
2018-05-06 19:52:34 INFO  ShutdownHookManager:54 - Shutdown hook called                   
2018-05-06 19:52:34 INFO  ShutdownHookManager:54 - Deleting directory /tmp/spark-e8a77988-d30c-4e96-81fe-bcaf5d565c75

但是,jar 中显然包含这个类:

1     " zip.vim version v28                                                                                                                                                                                                                                                                                                                                               
    1 " Browsing zipfile /home/[USER]/projects/scala_ts/out/artifacts/TimeSeriesFilter_jar/scala-ts.jar
    2 " Select a file with cursor and press ENTER
    3  
    4 META-INF/MANIFEST.MF
    5 com/
    6 com/stronghold/
    7 com/stronghold/HelloWorld$.class
    8 com/stronghold/TimeSeriesFilter$.class
    9 com/stronghold/DataSource.class
   10 com/stronghold/TimeSeriesFilter.class
   11 com/stronghold/HelloWorld.class
   12 com/stronghold/scratch.sc
   13 com/stronghold/HelloWorld$delayedInit$body.class

通常,这里挂断的是文件结构,但我很确定这里是正确的:

../
scala_ts/
| .git/
| .idea/
| out/
| | artifacts/
| | | TimeSeriesFilter_jar/
| | | | scala-ts.jar
| src/
| | main/
| | | scala/
| | | | com/
| | | | | stronghold/
| | | | | | DataSource.scala
| | | | | | HelloWorld.scala
| | | | | | TimeSeriesFilter.scala
| | | | | | scratch.sc
| | test/
| | | scala/
| | | | com/
| | | | | stronghold/
| | | | | | AppTest.scala
| | | | | | MySpec.scala                                                                                                                                                                                                                                                                                                                                                  
| target/
| README.md
| pom.xml

我在工作中运行了具有相同结构的其他工作(因此,不同的环境)。我现在正试图通过家庭项目获得更多便利,但这似乎是一个早期的挂断。

简而言之,我只是错过了一些明显的东西吗?

附录

有兴趣的可以看看我的pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stronghold</groupId>
  <artifactId>scala-ts</artifactId>
  <version>1.0-SNAPSHOT</version>
  <inceptionYear>2008</inceptionYear>
  <properties>
    <scala.version>2.11.8</scala.version>
  </properties>

  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.11.8</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.9</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.scala-tools.testing</groupId>
      <artifactId>specs_2.10</artifactId>
      <version>1.6.9</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.11</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.11</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-catalyst_2.11</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.3</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
          <args>
            <arg>-target:jvm-1.5</arg>
          </args>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <downloadSources>true</downloadSources>
          <buildcommands>
            <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
          </buildcommands>
          <additionalProjectnatures>
            <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
          </additionalProjectnatures>
          <classpathContainers>
            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
            <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
          </classpathContainers>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>

更新

对不够清晰表示歉意。我从与.jar (/home/[USER]/projects/scala_ts/out/artifacts/TimeSeriesFilter_jar/) 相同的目录中运行命令。也就是说,为了清楚起见,指定完整路径不会改变结果。

还应注意,我可以在 Intellij 中运行 HelloWorld,它使用相同的类引用 (com.stronghold.HelloWorld)。

【问题讨论】:

    标签: java scala apache-spark


    【解决方案1】:

    为什么不使用 jar 文件的路径以便spark-submit(与任何其他命令行工具一样)可以找到并使用它?

    鉴于路径 out/artifacts/TimeSeriesFilter_jar/scala-ts.jar 我会使用以下内容:

    spark-submit --class com.stronghold.HelloWorld out/artifacts/TimeSeriesFilter_jar/scala-ts.jar
    

    请注意,您应该位于项目的主目录中,该目录似乎是/home/[USER]/projects/scala_ts

    还请注意,我删除了 --master local[*],因为这是 spark-submit 使用的默认主 URL。

    【讨论】:

      【解决方案2】:

      恐怕这些都不是问题。我之前曾尝试删除项目中的所有内容并重新开始,但这也不起作用。一旦我想到开始一个完全不同的项目,它就工作得很好。显然 Intellij(我是其中的粉丝)决定在某个地方制造一个隐藏的问题。

      【讨论】:

        【解决方案3】:

        查看您的 pom 文件,您引用的 jar 文件与您在 pom 文件中的内容不匹配

          <artifactId>scala-ts</artifactId>
          <version>1.0-SNAPSHOT</version>
        

        pom 文件中的上述两行表明你的 jar 文件应该是scala-ts-1.0-SNAPSHOT.jar,但你只使用了scala-ts.jar。所以我假设你引用的是旧 jar。

        以下是您可以应用的几个步骤

        1. clean the project and package again
        2. make sure the jar file name by going to target folder of the project
        3. you can give the exact path to the target folder to point to the jar when you apply spark-submit command
        

        【讨论】:

        • 我又把罐子清理了一遍,打包了,但恐怕没什么影响。至于引用一个旧的jar,我只为这个项目创建了一个。为了安全起见,我删除了 jar 并从头开始构建一个新的。不幸的是,没有骰子。
        • 你检查了项目目标文件夹中的jar文件名吗?
        • 抱歉,这周我忙于工作。我只是回到这个话题。答案是肯定的,jar 名是正确的。
        • 正确是什么意思?能把jar文件名和完整路径分享一下吗?
        • 我的意思是没有其他的罐子。我建造的唯一一个在这里:../scala_ts/out/artifacts/TimeSeriesFilter_jar/scala-ts.jar。此外,目标文件夹中没有 jar,它们在构建时位于 out 文件夹中。
        【解决方案4】:

        尝试添加 ma​​ven-shade 插件并构建 > 运行。

        这是参考,可能对你有所帮助。

        Getting java.lang.ClassNotFoundException while running Spark Application

        【讨论】:

        • 您介意详细说明为什么这很有用吗?我不必在其他情况下使用 uber-jars。
        猜你喜欢
        • 1970-01-01
        • 2018-06-23
        • 2019-05-15
        • 2015-11-06
        • 2017-03-21
        • 2017-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多