简体   繁体   English

Spark / Java:GSON依赖项中的NoClassDefFoundError

[英]Spark/Java: NoClassDefFoundError in GSON dependency

I have a small Spark project using a JAR file, which I am packaging with Maven. 我有一个使用JAR文件的小型Spark项目,该文件与Maven打包在一起。

I am using GSON to work with JSON files, and I specifically need the JsonReader class. 我正在使用GSON处理JSON文件,并且特别需要JsonReader类。

I added the GSON dependency, and when I run it as a standard Java project it works fine. 我添加了GSON依赖项,当我将其作为标准Java项目运行时,它可以正常运行。 However, when I package it and run it in Spark it complains: 但是,当我打包并在Spark中运行它时,它会抱怨:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/stream/JsonReader

Here is the line in question: 这是有问题的行:

reader = new JsonReader(new InputStreamReader(new FileInputStream(
                jsonfile)));

I did import it: 我确实导入了它:

import com.google.gson.*;
import com.google.gson.stream.JsonReader;

Here is my POM file: 这是我的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SWETesting</groupId>
  <artifactId>SWETesting</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source/>
          <target/>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.8.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>
  </dependencies>
</project>

I even installed libgoogle-gson in my distribution, but that didn't help. 我什至在发行版中安装了libgoogle-gson,但这没有帮助。

What is the problem here? 这里有什么问题?

Edit: I am submitting the job in standalone mode after building it with Maven: 编辑:使用Maven构建作业后,我以独立模式提交作业:

mvn package
/home/chris/Tools/spark-1.2.1/bin/spark-submit --class PlagiarismCheck --master local[6] target/SWETesting-0.0.1-SNAPSHOT.jar > output.txt

The problem is that your project dependencies aren't included within SWETesting-0.0.1-SNAPSHOT.jar , hence the error. 问题是您的项目依赖项未包含在SWETesting-0.0.1-SNAPSHOT.jar ,因此会出现错误。 You can generate a fat jar using maven-assembly plugin or similar, that should fix the issue. 您可以使用maven-assembly插件或类似工具生成一个胖子罐,该胖子罐可以解决该问题。 You can find an example here . 您可以在此处找到示例。

Looks like it is referring to the old version of jar. 似乎是指jar的旧版本。 gson jar old version 1.5 doesn't have the package name stream. gson jar旧版本1.5没有软件包名称流。 Check the maven dependency tree and find if any older versions of gson jar exists. 检查Maven依赖关系树,查找是否存在gson jar的任何较旧版本。 If yes clean the project/ remove the jars in the repository 如果是,请清理项目/删除存储库中的罐子

Command to check the maven dependency tree 检查Maven依赖树的命令

C:\Projects\Project_name>mvn dependency:tree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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