Can't resolve the error: java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException

时间:2019-01-15 18:08:51

标签: java json maven

Trying to use Maven to organize my project and I keep running into the following error. I know that this error means the file is present at compile time but for some reason it can't be found at runtime.

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException

So I'm working on a project in Java that will take a user query, search Google Images and then download some of the results onto my computer. To that end I've had to use some third party libraries like JSoup, Json-Simple, and Gson.

I initially added Jsoup to my classpath manually and it worked, but then I heard about Maven and started using it instead. My issue is that when I try to run my code I get the error above.

I'm just not sure how to resolve this. I've seen a bunch of other posts about similar errors and I've tried to modify my pom.xml accordingly but I just can't get it to work. I've tried removing the ~/.m2 file, ran mvn clean, mvn install, mvn package, mvn compile, and it all works fine. But when it comes time to run, I keep getting that error.

Here's most of my pom.xml file.

<repositories>
    <repository>
    <id>central</id>
   <name>Maven repository</name>
   <url>http://repo1.maven.org/maven2</url>
 </repository>

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <!-- jsoup HTML parser library @ https://jsoup.org/ -->
  <groupId>org.jsoup</groupId>
  <artifactId>jsoup</artifactId>
  <version>1.11.3</version>
  <scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>fully.qualified.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>
</plugins>
</build>

FOUND SOLUTION: So I left out some parts of the pom.xml file to make it easier to read, and because all the other parts were generated by Maven itself so I figured there couldn't be an issue with anything there. But it adds a tag called "pluginManagement" that encloses all other plugins and apparently this does not allow the Shade plugin to run.

Between ngueno's guidance and this post Maven shade plugin is not called automatically for goal "package" I was able to figure out my issue, though I'm still not entirely sure why it is an issue. Anyways, I figured I'd update this post in case someone else with a similar problem stumbles across it. This was on Mac OS btw, in case it makes a difference. Thanks for your help everyone.

3 个答案:

答案 0 :(得分:0)

Skippy试一下netbeans,看看它是否出现相同的错误,希望如果它给出相同的错误,我们可以开始内部研究ya程序到底有什么问题,并希望能够解决ya问题:)

此处提供的链接可能会帮助您解决所面临的问题:https://crunchify.com/how-to-fix-java-lang-classnotfoundexception-org-springframework-web-servlet-dispatcherservlet-exception-spring-mvc-tomcat-and-404-error/

答案 1 :(得分:0)

通常NoClassDefFound错误与运行时缺少库有关。

由于我正在使用终端运行,所以您要使用Maven构建项目并运行生成的JAR,

我建议您使用maven-shade-plugin并生成一个超级罐子,如我在this question上所述。

生成uber-jar的目的是在其中携带所有必需的依赖项(可在应用程序类路径上找到)。

实施插件,然后尝试使用新的JAR运行。

PS:记得检查与{em>可执行罐子

相关的this section

更新:删除<scope>provided</scope>依赖项中的jsoup,以强制Maven将其打包到您的应用中,在提供的范围内,您说此依赖项将是由JDK在运行时提供。

答案 2 :(得分:0)

在依赖项中标识的jar必须存在于运行时类路径中。 这不是编译代码时可用的类路径; 这是运行应用程序的主机上的类路径。

您必须在目标主机上安装这些jar。

编辑:更多详细信息

您必须执行以下操作:

  1. 标识运行时主机。
  2. 在运行时主机上创建一个目录,您将在其中安装相关的jar文件。
  3. 在类路径中包含每个jar。

请考虑放弃“自行劳碌”的道路。 如果您使用Spring Boot (我喜欢, 我不为他们工作)。 Spring Boot的一项功能是重新设计的“胖罐”,它将在一个可交付工件(胖罐)中包含依赖项,并将其在启动时添加到类路径中。