2016-10-14 7 views
1

この質問は続きthis threadです。java.lang.NoClassDefFoundError:com/typesafe/config/ConfigFactoryを使ってScalaプロジェクトをMavenでパッケージ化する場合

私が問題としているのは、Mavenを使ってScala Sparkプロジェクトをパッケージ化することです。私は、このコマンドを実行すると

:これは(I上記のスレッドからの勧告を検討する私の現在のpom.xmlある

Exception in thread "main" java.lang.NoClassDefFoundError: com/typesafe/config/ConfigFactory 
    at org.test.consumer.kafka.KafkaConsumer.<init>(KafkaConsumer.scala:38) 
    at org.test.consumer.TestRunner$.main(TestRunner.scala:19) 
    at org.test.consumer.TestRunner.main(TestRunner.scala) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:729) 
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185) 
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210) 
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124) 
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) 
Caused by: java.lang.ClassNotFoundException: com.typesafe.config.ConfigFactory 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 12 more 

spark-submit --name 28 --master local[2] --class org.test.consumer.TestRunner \ 
/usr/tests/test1/target/test_service-1.0-SNAPSHOT.jar \ 
$arg1 $arg2 $arg3 $arg4 $arg5 

を...、私は次のエラーを取得します生成されたjarにスカラクラスが含まれていることを確認してください)。

<?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>org.test.consumer</groupId> 
    <artifactId>test_service</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 

     <java.version>1.8</java.version> 
     <scala.version>2.11.8</scala.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.scala-lang</groupId> 
      <artifactId>scala-library</artifactId> 
      <version>${scala.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.spark</groupId> 
      <artifactId>spark-streaming_2.11</artifactId> 
      <version>1.6.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.spark</groupId> 
      <artifactId>spark-streaming-kafka_2.11</artifactId> 
      <version>1.6.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.module</groupId> 
      <artifactId>jackson-module-scala_2.11</artifactId> 
      <version>2.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.sedis</groupId> 
      <artifactId>sedis_2.11</artifactId> 
      <version>1.2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.lambdaworks</groupId> 
      <artifactId>jacks_2.11</artifactId> 
      <version>2.3.3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.spark</groupId> 
      <artifactId>spark-sql_2.11</artifactId> 
      <version>1.6.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.spark</groupId> 
      <artifactId>spark-mllib-local_2.11</artifactId> 
      <version>2.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.github.nscala-time</groupId> 
      <artifactId>nscala-time_2.11</artifactId> 
      <version>2.12.0</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <!-- Configure maven-compiler-plugin to use the desired Java version --> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>${java.version}</source> 
        <target>${java.version}</target> 
       </configuration> 
      </plugin> 

      <!-- Use build-helper-maven-plugin to add Scala source and test source directories --> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>build-helper-maven-plugin</artifactId> 
       <version>1.10</version> 
       <executions> 
        <execution> 
         <id>add-source</id> 
         <phase>generate-sources</phase> 
         <goals> 
          <goal>add-source</goal> 
         </goals> 
         <configuration> 
          <sources> 
           <source>src/main/scala</source> 
          </sources> 
         </configuration> 
        </execution> 
        <execution> 
         <id>add-test-source</id> 
         <phase>generate-test-sources</phase> 
         <goals> 
          <goal>add-test-source</goal> 
         </goals> 
         <configuration> 
          <sources> 
           <source>src/test/scala</source> 
          </sources> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

      <!-- Use scala-maven-plugin for Scala support --> 
      <plugin> 
       <groupId>net.alchim31.maven</groupId> 
       <artifactId>scala-maven-plugin</artifactId> 
       <version>3.2.2</version> 
       <executions> 
        <execution> 
         <goals> 
          <!-- Need to specify this explicitly, otherwise plugin won't be called when doing e.g. mvn compile --> 
          <goal>compile</goal> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 
+0

'com/typesafe/config/ConfigFactory'はライブラリに属しています。そのライブラリをpom.xmlに含めましたか? –

答えて

5

あなたのpom.xml。

<dependency> 
    <groupId>com.typesafe</groupId> 
    <artifactId>config</artifactId> 
    <version>1.3.1</version> 
</dependency> 

このjarファイルがすでに存在する場合は、追加する必要はありません。

jarは、jarファイル内にパッケージされたすべての依存関係を持つ実行可能なjarである必要があります。 これを行うには、maven assembly pluginを使用する必要があります。

<build> 
    <plugins> 
     <plugin> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
      </descriptorRefs> 
      <archive> 
      <manifest> 
       <mainClass>org.sample.App</mainClass> // put your main class here. 
      </manifest> 
      </archive> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 

このプラグインは、(離れて、通常のjarファイルから)-jar-with-dependenciesを付加した名前を持つ追加のjarファイルを作成します。オリジナルのjarを使用する代わりに、このjarを使用します。

+0

ええと、jarファイルのクリーニングとパッケージング後も同じ問題です。 BTW私はプロジェクトのパスに手動でjarファイルを追加しませんでした。以前はこの依存関係を追加することなくコンパイルしました。だから、他の依存関係で上書きされているかもしれませんか?また、 'kafka.utils.Logging'でも同じ問題が発生しましたが、コメントしました。 – FiofanS

+0

おそらく、Jarファイル内にも依存関係をパッケージ化する必要があります。私は私の答えを更新しました。 –

+0

私はそれが最終的に働いたとは信じられない!どうもありがとうございます!私は 'test_service-1.0-SNAPSHOT-jar-with-dependencies.jar'を使っています。 – FiofanS

関連する問題